2010-01-21 65 views
6

我有意冒充众所周知的Web服务和WCF服务进行集成测试。为此,我想捕获服务元数据,自动生成服务存根,并在自托管环境中托管服务存根。将ASMX Web服务元数据导入到WCF端点

以下this article here,我能够获得远程Wcf服务元数据并生成合同。但是,对于远程的Asmx Web服务,我也遇到了一些困难。

我有一套用于审核的mickey-mouse解决方案。

我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> 
+0

@johnny - 不客气。 ... 好家伙!我的第一个赏金! – Filburt 2010-02-22 22:12:55

回答

6

让它与ASMX Web服务的工作方式是指定使用MetadataExchangeClientModeMetadataExchangeClientMode

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

HttpGet为您的ASMX服务 和MetadataExchangeClientModeMetadataExchange为您的WCF服务。

+0

@Filburt,muchos gracias! – 2010-02-22 15:00:59