简体   繁体   English

将ASMX合同转换为WCF

[英]Converting ASMX contracts to WCF

I have an older webservice I need to convert into a new system which isn't hosted in IIS, so I can't copy it directly. 我有一个较旧的Web服务,需要转换为不在IIS中托管的新系统,因此无法直接复制它。 I can create self hosted WCF services so I could put the functionality there instead. 我可以创建自托管的WCF服务,因此可以将功能放在那里。 However, I can't get the contracts to match up as the client we already have gives the following exception when connecting to the webservice: 但是,由于我们已经拥有的客户端在连接到Web服务时给出以下异常,因此我无法使合同匹配:

The message with action ' http://services.mysite.com/MyService/MyMethod ' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. 由于EndpointDispatcher的ContractFilter不匹配,因此无法在接收方处理带有操作“ http://services.mysite.com/MyService/MyMethod ”的消息。

I read in some other SO posts that I could ensure the contract would be the same by running the following in a command prompt: 我读过其他一些SO帖子,我可以通过在命令提示符中运行以下命令来确保合同相同:

wsdl.exe /serverInterface http://oldservices.mysite.com/MyService.asmx?WSDL

This command generates some code which, according to the documentation , "Generates interfaces for server implementation". 该命令生成一些代码, 根据文档 “为服务器实现生成接口”。

I took this code into my new solution and got it to start (after adding some extra attributes, and also prettifying the code), and I can call MyMethod successfully from WebService Studio and get the expected results. 我将此代码带入我的新解决方案中,并使其开始运行(在添加了一些额外的属性并修饰了代码之后),我可以从WebService Studio成功调用MyMethod并获得预期的结果。 However, the old client still complains with the same exception. 但是,老客户仍然抱怨相同的例外。

What must I do to successfully convert an old ASMX webservice into a fully compatible WCF service? 要成功将旧的ASMX Web服务成功转换为完全兼容的WCF服务,我该怎么做?

WSDL of old ASMX webservice @ Pastebin 旧ASMX Web服务的WSDL @ Pastebin

Generated Server Interface (from wsdl.exe) @ Pastebin 生成的服务器接口(从wsdl.exe)@ Pastebin

My adjusted interface 我的调整界面

[WebServiceBinding(Name = "MyServiceSoap", Namespace = "http://services.mysite.com/MyService")]
[XmlInclude(typeof(Foo))]
[XmlInclude(typeof(Bar))]
[ServiceContract]
[ServiceKnownType(typeof(Foo))]
[ServiceKnownType(typeof(Bar))]
public interface IMyServiceSoap {

    [OperationContract, XmlSerializerFormat(Style = OperationFormatStyle.Document)]
    [WebMethod]
    [SoapDocumentMethod("http://services.mysite.com/MyService/MyMethod", RequestNamespace = "http://services.mysite.com/MyService", ResponseNamespace = "http://services.mysite.com/MyService", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

    ReturnMessage MyMethod();
}

My adjusted type definitions 我调整后的类型定义

[Serializable]
[XmlType(Namespace = "http://services.mysite.com/MyService")]
public class ReturnMessage {
    public ReturnStatus Status { get; set; }
    public object GenericReturn { get; set; }
}

[Serializable]
[XmlType(Namespace = "http://services.mysite.com/MyService")]
public enum ReturnStatus {
}

[Serializable]
[XmlType(Namespace = "http://services.mysite.com/MyService")]
public class Foo {
    public string Text { get; set; }
}

[Serializable]
[XmlType(Namespace = "http://services.mysite.com/MyService")]
public class Bar {
    public int Number{ get; set; }
}

Implementation class 实现类

public class MyServiceSoap : IMyServiceSoap {
    public ReturnMessage MyMethod() {
        return new ReturnMessage() { Status = ReturnStatus.OK, GenericReturn = new Foo() };
    }
}

WCF starter WCF启动器

ServiceHost host = new ServiceHost(typeof(MyServiceSoap), new Uri("http://localhost:8080/MyService.asmx"));
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy12;
host.Description.Behaviors.Add(smb);
host.Open();

I finally got a push in the right direction on another forum, which pointed me to this page . 我终于在另一个论坛上朝着正确的方向努力,这使我指向了此页面 What I had previously found about wsdl.exe proved to be incorrect. 我以前对wsdl.exe发现被证明是不正确的。 Instead you have to run the following command: 相反,您必须运行以下命令:

SvcUtil.exe /serviceContract http://oldservices.mysite.com/MyService.asmx?WSDL

This generates a file you can adjust and prettify (just don't change any of the classnames). 这将生成一个可以调整和修饰的文件(只是不要更改任何类名)。

Then start the WCF service as I did above, with one additional line: 然后像上面一样启动WCF服务,并增加一行:

smb.ExternalMetadataLocation = new Uri("http://localhost:8080/wsdl/MyService.wsdl");

The WSDL file referred there has to be the same WSDL as your old service generates. 引用的WSDL文件必须与您的旧服务生成的WSDL文件相同。 Just save the autogenerated WSDL file to your new server and remember to change address locations at the bottom of the file to point to your new service (instead of your old one) and you should be ready to go! 只需将自动生成的WSDL文件保存到新服务器,并记得更改文件底部的地址位置以指向您的新服务(而不是旧服务),就可以开始使用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM