2012-04-27 26 views
1

私はASP.net Webサービスと共通のプロキシを使用してWCFサービスを消費する必要があるC#コンソールアプリケーションを作成します。 WCFとASP.net Webサービスは同じデータとサービス契約と同じ名前空間を実装します。 どのように達成できるかを知る必要があります。一般的な参照を使用してASP.net WebサービスとWCF Webサービスを消費する方法

あなたの提案や意見をお寄せいただきありがとうございます。

@Pertu入力いただきありがとうございますが、私は同じアプローチを使用していますが、同じ消費者は両方のサービスを呼び出すことができません。以下は、Webサービスを呼び出すために消費者によって使用されるコードのスナップショット

=======プロキシは

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Web.Services.WebServiceBindingAttribute(Name="BaseServiceSoap", 
Namespace="http://tempuri.org/")] 

public partial class BaseService : System.Web.Services.Protocols.SoapHttpClientProtocol 
{ 

private System.Threading.SendOrPostCallback GetWeightOperationCompleted; 

/// <remarks/> 
public BaseService() 
{ 
    this.Url = "http://localhost:53899/BaseService.asmx"; 
} 
...... 

======== Consumer Code 
//Below call to ASP.NEt webservice succeeds 

BaseService typeACalculator = new BaseService(); 
typeACalculator.Url = "http://localhost:54106/TypeA.asmx"; 
Console.WriteLine("Article for type A weighs: " + 
    typeACalculator.GetWeight(articleForCalcA).ToString()); 

//Below call to WCF fails 

BaseService WCFCalculator = new BaseService(); 

WCFCalculator.Url = "http://localhost:52224/TypeWCF35Calc.svc"; 
Console.WriteLine("Article for type WCF weighs: " + 
    WCFCalculator.GetWeight(articleForCalcB).ToString()); 
Console.ReadKey(); 

======== Contract in WCF=============== 

[ServiceContract] 
public interface IBaseServiceSoap 
{ 
    [OperationContract] 
    double GetWeight(Article article); 
} 

====== Contract in ASP.NEt webservice================ 
/// <remarks/> 


    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")] 
    [System.Web.Services.WebServiceBindingAttribute(Name="BaseServiceSoap", 
    Namespace="http://tempuri.org/")] 
    public interface IBaseServiceSoap 
    { 

    /// <remarks/> 
    [System.Web.Services.WebMethodAttribute()] 
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute 
    ("http://tempuri.org/GetWeight",  
    RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", 
    Use=System.Web.Services.Description.SoapBindingUse.Literal, 
    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 

    double GetWeight(Article article); 
    }  

==========ですうーん、いつかのために道路にされていました。 ...ここ はASMXのWebサービスのWSDLを

=== WSDLは==== Webサービスのはず美しさは、それがどのような問題ではないということですWSDL.exe

<?xml version="1.0" encoding="utf-8"?> 
<wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
xmlns:s="http://www.w3.org/2001/XMLSchema" 
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://tempuri.org/" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 
    <wsdl:types> 
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> 
     <s:element name="GetWeight"> 
     <s:complexType> 
      <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="article" type="tns:Article" /> 
      </s:sequence> 
     </s:complexType> 
     </s:element> 
     <s:complexType name="Article"> 
     <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="Id" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Characteristics" 
      type="tns:ArticleCharacteristics" /> 
     </s:sequence> 
     </s:complexType> 
     <s:complexType name="ArticleCharacteristics"> 
     <s:sequence> 
      <s:element minOccurs="1" maxOccurs="1" name="Length" type="s:double" /> 
      <s:element minOccurs="1" maxOccurs="1" name="Height" type="s:double" /> 
      <s:element minOccurs="1" maxOccurs="1" name="Width" type="s:double" /> 
      <s:element minOccurs="1" maxOccurs="1" name="Weight" type="s:double" /> 
     </s:sequence> 
     </s:complexType> 
     <s:element name="GetWeightResponse"> 
     <s:complexType> 
      <s:sequence> 
      <s:element minOccurs="1" maxOccurs="1" name="GetWeightResult" type="s:double" /> 
      </s:sequence> 
     </s:complexType> 
     </s:element> 
    </s:schema> 
    </wsdl:types> 
    <wsdl:message name="GetWeightSoapIn"> 
    <wsdl:part name="parameters" element="tns:GetWeight" /> 
    </wsdl:message> 
    <wsdl:message name="GetWeightSoapOut"> 
    <wsdl:part name="parameters" element="tns:GetWeightResponse" /> 
    </wsdl:message> 
    <wsdl:portType name="BaseServiceSoap"> 
    <wsdl:operation name="GetWeight"> 
     <wsdl:input message="tns:GetWeightSoapIn" /> 
     <wsdl:output message="tns:GetWeightSoapOut" /> 
    </wsdl:operation> 
    </wsdl:portType> 
    <wsdl:binding name="BaseServiceSoap" type="tns:BaseServiceSoap"> 
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="GetWeight"> 
     <soap:operation soapAction="http://tempuri.org/GetWeight" style="document" /> 
     <wsdl:input> 
     <soap:body use="literal" /> 
     </wsdl:input> 
     <wsdl:output> 
     <soap:body use="literal" /> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:binding name="BaseServiceSoap1" type="tns:BaseServiceSoap"> 
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="GetWeight"> 
     <soap12:operation soapAction="http://tempuri.org/GetWeight" style="document" /> 
     <wsdl:input> 
     <soap12:body use="literal" /> 
     </wsdl:input> 
     <wsdl:output> 
     <soap12:body use="literal" /> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:service name="TypeA"> 
    <wsdl:port name="BaseServiceSoap" binding="tns:BaseServiceSoap"> 
     <soap:address location="http://localhost:54106/TypeA.asmx" /> 
    </wsdl:port> 
    <wsdl:port name="BaseServiceSoap1" binding="tns:BaseServiceSoap1"> 
     <soap12:address location="http://localhost:54106/TypeA.asmx" /> 
    </wsdl:port> 
    </wsdl:service> 
</wsdl:definitions> 

===== WSDL of WCF webservice, generated after configuring the WCF webservice=== 
<?xml version="1.0" encoding="utf-8"?> 
<wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
xmlns:s="http://www.w3.org/2001/XMLSchema" 
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://tempuri.org/" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 
    <wsdl:types> 
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> 
     <s:element name="GetWeight"> 
     <s:complexType> 
      <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="article" type="tns:Article" /> 
      </s:sequence> 
     </s:complexType> 
     </s:element> 
     <s:complexType name="Article"> 
     <s:sequence> 
      <s:element minOccurs="0" maxOccurs="1" name="Id" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="Characteristics" 
      type="tns:ArticleCharacteristics" /> 
     </s:sequence> 
     </s:complexType> 
     <s:complexType name="ArticleCharacteristics"> 
     <s:sequence> 
      <s:element minOccurs="1" maxOccurs="1" name="Length" type="s:double" /> 
      <s:element minOccurs="1" maxOccurs="1" name="Height" type="s:double" /> 
      <s:element minOccurs="1" maxOccurs="1" name="Width" type="s:double" /> 
      <s:element minOccurs="1" maxOccurs="1" name="Weight" type="s:double" /> 
     </s:sequence> 
     </s:complexType> 
     <s:element name="GetWeightResponse"> 
     <s:complexType> 
      <s:sequence> 
      <s:element minOccurs="1" maxOccurs="1" name="GetWeightResult" type="s:double" /> 
      </s:sequence> 
     </s:complexType> 
     </s:element> 
    </s:schema> 
    </wsdl:types> 
    <wsdl:message name="GetWeightSoapIn"> 
    <wsdl:part name="parameters" element="tns:GetWeight" /> 
    </wsdl:message> 
    <wsdl:message name="GetWeightSoapOut"> 
    <wsdl:part name="parameters" element="tns:GetWeightResponse" /> 
    </wsdl:message> 
    <wsdl:portType name="BaseServiceSoap"> 
    <wsdl:operation name="GetWeight"> 
     <wsdl:input message="tns:GetWeightSoapIn" /> 
     <wsdl:output message="tns:GetWeightSoapOut" /> 
    </wsdl:operation> 
    </wsdl:portType> 
    <wsdl:binding name="BaseServiceSoap" type="tns:BaseServiceSoap"> 
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="GetWeight"> 
     <soap:operation soapAction="http://tempuri.org/GetWeight" style="document" /> 
     <wsdl:input> 
     <soap:body use="literal" /> 
     </wsdl:input> 
     <wsdl:output> 
     <soap:body use="literal" /> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:binding name="BaseServiceSoap1" type="tns:BaseServiceSoap"> 
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="GetWeight"> 
     <soap12:operation soapAction="http://tempuri.org/GetWeight" style="document" /> 
     <wsdl:input> 
     <soap12:body use="literal" /> 
     </wsdl:input> 
     <wsdl:output> 
     <soap12:body use="literal" /> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:service name="TypeA"> 
    <wsdl:port name="BaseServiceSoap" binding="tns:BaseServiceSoap"> 
     <soap:address location="http://localhost:54106/TypeA.asmx" /> 
    </wsdl:port> 
    <wsdl:port name="BaseServiceSoap1" binding="tns:BaseServiceSoap1"> 
     <soap12:address location="http://localhost:54106/TypeA.asmx" /> 
    </wsdl:port> 
    </wsdl:service> 
</wsdl:definitions> 

答えて

1

を使用して作成されていますクライアントは何ですかサービスの基礎となる実装。

あなたが望む、またはできるだけ多くの方法であなたのプロキシを構築します(多くはWCFを使用すると言います)。私はSOAPアドレスが異なっていると仮定しますので、(コマンドライン引数で)それをパラメータ化することを確認してください...

+0

入力いただきありがとうございますが、私は同じアプローチを使用していますが、同じコンシューマは両方のサービスを呼び出すことができません。以下はコードスナップショット – Sam

+0

@Samです。それがうまくいかなければ契約は同じではありません。また、ワイヤ上のいくつかのことはうまくいきません。私はこれを提案します:各実装のために働くクライアントを生成してください。成功したメッセージ交換のためにワイヤーダンプを比較し、そこからそれを取るために、フィドラーを使用してください。あるいは、両方の実装用にWSDLを生成して、コードの代わりにポストすることができますか? –

0

はい、WCFサービスを使用できます。&同じコードを使用しているASP.NET WebService。

ステップ1-まず、SOAPヘッダーをインターセプトするためにWCFサービスを変更する必要があります。それにはWCFExtrasを使用してください。

ステップ2- ASP.NET Webサービスを使用してプロキシを生成します。

関連する問題