将 ASMX Web 服务元数据导入到 WCF 端点

发布于 2024-08-19 05:57:32 字数 3805 浏览 2 评论 0原文

我有兴趣模拟著名的 Web 服务和 Wcf 服务以进行集成测试。为此,我想在自托管环境中捕获服务元数据、自动生成服务存根和托管服务存根。

按照本文,我能够获取远程 Wcf 服务元数据并生成合同。但是,我在对远程 Asmx Web 服务执行相同操作时遇到一些困难。

我有一套米老鼠解决方案来审查这个问题。

我的 Asmx 解决方案包含一个默认的“Hello World”Web 服务,可以在下面找到

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class SimpleAsmxService : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld () { return "Hello World"; }
}

我的 Wcf 解决方案包含一个默认的“Hello World”服务,也可以在下面找到

[ServiceContract]
public interface ISimpleWcfService
{
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

[DataContract]
public class CompositeType
{
    [DataMember]
    public bool BoolValue { get; set; }

    [DataMember]
    public string StringValue { get; set; }
}

public class SimpleWcfService : ISimpleWcfService
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

最后,这个小控制台看起来像

class Program
{
    public const string UrlWcf = 
        "http://localhost:8731/Design_Time_Addresses/SimpleWcfService/mex";
    public const string UrlAsmx = 
        "http://localhost:1803/SimpleAsmxService.asmx?WSDL";

    static void Main(string[] args)
    {
        EndpointAddress mexAddress = new EndpointAddress (UrlWcf);
        MetadataExchangeClient mexClient = 
            new MetadataExchangeClient (mexAddress);
        mexClient.ResolveMetadataReferences = true;

        // NOTE: blows up if we use UrlAsmx
        MetadataSet metaSet = mexClient.GetMetadata ();

        WsdlImporter importer = new WsdlImporter (metaSet);
        Collection<ContractDescription> contracts = 
            importer.ImportAllContracts();
    }
}

在我看来,我应该能够从著名的 Asmx Web 服务中提取 Wsdl 并生成合约 [以及从合约到代码],但似乎无法扭曲前面的示例来执行此操作。任何帮助将不胜感激,

谢谢!


注意:调用上面的 MetadataSet metaSet = mexClient.GetMetadata(); 时生成的错误是 System.InvalidOperationException ,消息为

元数据包含无法解析的引用:'http://localhost:1803/SimpleAsmxService.asmx ?WSDL'

带有 System.InvalidOperationException 内部异常,消息为

<?xml version="1.0" encoding="utf-16"?>
<Fault xmlns="http://www.w3.org/2003/05/soap-envelope">
    <Code>
        <Value>Sender</Value>
    </Code>
    <Reason>
        <Text xml:lang="en">
System.Web.Services.Protocols.SoapException: Unable to handle request without a valid action parameter. Please supply a valid soap action.
   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)
        </Text>
    </Reason>
</Fault>

I am interested in impersonating well-known Web Services and Wcf Services for integration test purposes. To this end, I would like to capture service metadata, auto-generate service stubs, and host service stubs in a self-hosted environment.

Following this article here, I am able to obtain remote Wcf Service metadata and generate contracts. However, I am having some difficulty doing the same for remote Asmx Web Services.

I have a set of mickey-mouse solutions for vetting this out.

My Asmx solution contains a default "Hello World" web service, found below

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class SimpleAsmxService : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld () { return "Hello World"; }
}

My Wcf solution contains a default "Hello World" service, also found below

[ServiceContract]
public interface ISimpleWcfService
{
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

[DataContract]
public class CompositeType
{
    [DataMember]
    public bool BoolValue { get; set; }

    [DataMember]
    public string StringValue { get; set; }
}

public class SimpleWcfService : ISimpleWcfService
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

Finally, the little console-that-could looks like

class Program
{
    public const string UrlWcf = 
        "http://localhost:8731/Design_Time_Addresses/SimpleWcfService/mex";
    public const string UrlAsmx = 
        "http://localhost:1803/SimpleAsmxService.asmx?WSDL";

    static void Main(string[] args)
    {
        EndpointAddress mexAddress = new EndpointAddress (UrlWcf);
        MetadataExchangeClient mexClient = 
            new MetadataExchangeClient (mexAddress);
        mexClient.ResolveMetadataReferences = true;

        // NOTE: blows up if we use UrlAsmx
        MetadataSet metaSet = mexClient.GetMetadata ();

        WsdlImporter importer = new WsdlImporter (metaSet);
        Collection<ContractDescription> contracts = 
            importer.ImportAllContracts();
    }
}

It seems to me that I should be able to pull Wsdl from a well-known Asmx Web Service and generate contracts [and from contracts to code], but cannot seem to contort the preceding sample to do so. Any help would be much appreciated,

Thanks!


NOTE: the error generated when invoking MetadataSet metaSet = mexClient.GetMetadata(); above is a System.InvalidOperationException with message of

Metadata contains a reference that cannot be resolved : 'http://localhost:1803/SimpleAsmxService.asmx?WSDL'

With a System.InvalidOperationException inner exception with message of

<?xml version="1.0" encoding="utf-16"?>
<Fault xmlns="http://www.w3.org/2003/05/soap-envelope">
    <Code>
        <Value>Sender</Value>
    </Code>
    <Reason>
        <Text xml:lang="en">
System.Web.Services.Protocols.SoapException: Unable to handle request without a valid action parameter. Please supply a valid soap action.
   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
        </Text>
    </Reason>
</Fault>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2024-08-26 05:57:32

让它与 ASMX Web 服务配合使用的方法是

...
MetadataExchangeClient mexClient = 
    new MetadataExchangeClient (new Uri(), MetadataExchangeClientMode.HttpGet);
...

使用 MetadataExchangeClientMode.HttpGet 为您的 ASMX 服务 指定 MetadataExchangeClientMode
以及用于 WCF 服务的 MetadataExchangeClientMode.MetadataExchange

The way to get it to work with an ASMX web service is to specify the MetadataExchangeClientMode

...
MetadataExchangeClient mexClient = 
    new MetadataExchangeClient (new Uri(), MetadataExchangeClientMode.HttpGet);
...

using MetadataExchangeClientMode.HttpGet for your ASMX services
and MetadataExchangeClientMode.MetadataExchange for your WCF services.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文