簡體   English   中英

使用Soap客戶端時出現InvalidOperationException

[英]InvalidOperationException when using soap client

我已經在vs2008中使用添加服務引用對話框作為wsdl文件添加了。

MyService serviceproxy = new MyService();

實例化服務代理時,我收到一個InvalidOperationException及其以下文本(從德語翻譯而來):

在服務模型引用客戶端配置部分中,找不到合同“ ServiceName.ServiceInterface”的默認終結點元素。 這可能是因為:未找到應用程序配置文件,或者未在客戶端元素項中找到與該合同相對應的端點。

其中,servicename是我在vs2008中添加服務時提供的服務名稱,而ServiceInterface是為其自動生成的接口。

編輯這里是我的app.config中的內容:

<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="MyServiceBinding" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
    </system.serviceModel>

您需要在配置中添加以下內容:

<client>
  <endpoint binding="basicHttpBinding" 
    bindingConfiguration="MyServiceBinding" contract="ServiceName.ServiceInterface"
    name="MyServiceEndpoint">
  </endpoint>
</client>

在您的標簽內

我剛剛讀了你的評論。

因此,從端點配置中刪除了地址。

您可以選擇完全在代碼中指定端點,也可以僅指定地址,如下所示:

MyServiceClient proxy = new MyServiceClient();
proxy.Endpoint.Address = new EndpointAddress ("http://addressto your service"); //<-- address

檢查您的配置文件-web.config(如果您位於ASP.NET Web應用程序或網站中),app.config(如果它是Winforms或控制台應用程序)。

在那里應該為您的WCF服務進行一些配置- <system.serviceModel>以下的任何內容都可以。 如果沒有-將必要的信息添加到您的配置中!

好的,因此,如果要在代碼中指定端點URL,則在實例化客戶端代理類時需要執行此操作-否則,它將在config中查找。 使用此代碼段,您將使用app.config中的http綁定配置設置,並在代碼中分別指定URL:

BasicHttpBinding binding = new BasicHttpBinding("MyServiceBinding");
EndpointAddress address = new EndpointAddress(new Uri("http://localhost:8888/YourService"));

MyService serviceproxy = new MyService(binding, address);

這樣,basicHttpBinding對象將在name=MyServiceBinding的綁定下從配置中讀取設置。

編輯:對不起,我的第一個答案是錯誤的。 對於客戶,您需要:

ChannelFactory<Interface> factory = new ChannelFactory< YourServiceInterface >(new basicHttpBinding(), new EndpointAddress(new Uri("http://localhost:8888/YourService")));
YourServiceInterface proxy = factory.CreateChannel();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM