2012-08-23 67 views
1

我有一個看起來像這樣的asmx服務:移動ASMX服務,WCF基本綁定服務

[WebService(Namespace = "http://www.zzz.com/ema/xml/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
public class AService : System.Web.Services.WebService, IAServiceSoap 

的IAServiceSoap接口看看下面

[WebService(Namespace = "http://www.zzz.com/ema/xml/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    public class AService : System.Web.Services.WebService, IAServiceSoap 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")] 
[System.Web.Services.WebServiceBindingAttribute(Name = "AsmbServiceSoap", Namespace = "http://www.zzz.com/ema/xml/")] 
[System.Xml.Serialization.SoapIncludeAttribute(typeof(SOAPXmlEventEntry))] 
public interface IAsmbServiceSoap { 

    /// <remarks/> 
    [System.Web.Services.WebMethodAttribute()] 
    [System.Web.Services.Protocols.SoapRpcMethodAttribute("http://www.zzz.com/ema/xml/Login", RequestNamespace="http://www.zzz.com/ema/xml/", ResponseNamespace="http://www.zzz.com/ema/xml/")] 
    void Login(string url, string id, int ClientType, out int ClientID); 


} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.SoapTypeAttribute(Namespace="http://www.zzz.com/ema/xml/")] 
public partial class SOAPXmlEventEntry { 

    private int datatypeField; 

    private string dataField; 

    private int idField; 

    /// <remarks/> 
    public int datatype { 
     get { 
      return this.datatypeField; 
     } 
     set { 
      this.datatypeField = value; 
     } 
    } 

    /// <remarks/> 
    public string data { 
     get { 
      return this.dataField; 
     } 
     set { 
      this.dataField = value; 
     } 
    } 

    /// <remarks/> 
    public int id { 
     get { 
      return this.idField; 
     } 
     set { 
      this.idField = value; 
     } 
    } 
} 

的問題是我怎麼動的東西,看起來像這樣WCF的Web服務與基本綁定?

+0

對於初學者來說,可以使用一組不同的屬性:'ServiceContract'和'OperationContract'。你的配置現在主要存在於你的'app.config'中的[](http://msdn.microsoft.com/en-us/library/ms731354.aspx)中。 – EthanB

+0

但我如何設置所有這些名稱空間? –

+0

您可以在屬性中設置命名空間,請參閱http://rebuildall.umbraworks.net/2010/11/10/WCF_service_namespaces – stuartd

回答

2

如果在WCF中使用控制檯應用程序實現,我嘲笑你的服務看起來像什麼。只要您使用.NET 4.0,WCF的簡化配置將負責爲服務主機創建端點。由於主機的基礎URL基於http協議,因此將使用「basicHttpBinding」作爲默認綁定。

對於客戶端代碼,您只需使用用於創建ServiceHost的URL添加服務引用即可。在這種情況下,我添加了一個服務引用並將其放入名稱空間「AService」中。

主機代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.ServiceModel.Description; 

namespace WcfHost 
{ 
    [ServiceContract(Namespace="http://www.zzz.com/ema/xml/")] 
    public interface IAsmbServiceSoap 
    { 
     [OperationContract] 
     void Login(string url, string id, int ClientType, out int ClientID); 
    } 

    public class AService : IAsmbServiceSoap 
    { 
     #region IAsmbServiceSoap Members 

     public void Login(string url, string id, int ClientType, out int ClientID) { 
      ClientID = 100; 
      // do work 
     } 

     #endregion 
    } 


    class Program 
    { 
     static void Main(string[] args) { 

      ServiceHost serviceHost = new ServiceHost(typeof(AService), new Uri("http://localhost:8000/AService")); 

      ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior() { HttpGetEnabled = true }; 
      serviceHost.Description.Behaviors.Add(metadataBehavior); 

      serviceHost.Open(); 

      Console.WriteLine("Open for communication. Press ENTER to close."); 
      Console.ReadLine(); 

      serviceHost.Close(); 
     } 
    } 
} 

樣品的客戶端代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace WcfClient 
{ 

    class Program 
    { 
     static void Main(string[] args) { 

      AService.AsmbServiceSoapClient client = new AService.AsmbServiceSoapClient(); 

      int clientId = client.Login("http://www.someurl.com", "test", 1); 

      Console.WriteLine(clientId); 
      Console.ReadLine(); 
     } 
    } 
}