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

时间:2010-01-21 19:36:53

标签: wcf web-services asmx metadata

我感兴趣的是冒充知名的Web服务和Wcf服务进行集成测试。为此,我想在自托管环境中捕获服务元数据,自动生成服务存根和主机服务存根。

this article here之后,我能够获取远程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>

1 个答案:

答案 0 :(得分:6)

使其与ASMX Web服务一起使用的方法是指定 MetadataExchangeClientMode

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

为您的ASMX服务使用 MetadataExchangeClientMode HttpGet MetadataExchangeClientMode MetadataExchange ,用于您的WCF服务。