将ASMX服务移动到WCF基本绑定服务

时间:2012-08-23 14:43:09

标签: c# .net wcf web-services

我的asmx服务看起来像这样:

[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 webservice?

1 个答案:

答案 0 :(得分: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();
        }
    }
}