2010-03-23 89 views
2

我已經使用vs2008中的添加服務引用對話框添加爲wsdl文件。使用soap客戶端時出現InvalidOperationException

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

發表您的配置文件在這裏和你的ServiceContract接口 – albertjan 2010-03-23 10:15:08

+0

我已經更新了我的問題與我的app.config的內容的名稱。但請注意,我計劃在代碼中指定服務url,而不是在配置中,因爲url是用戶輸入。 – codymanix 2010-03-23 10:20:42

回答

2

你需要在你的配置是這樣的:你的標籤

我剛剛看了你的評論裏面

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

@marc_s:它說:contract =「ServiceName.ServiceInterface」 – albertjan 2010-03-23 12:56:11

1

檢查您的配置文件 - web.config文件,如果你在一個ASP.NET Web應用程序或網站的時候,如果的app.config它的一個Winforms或控制檯應用程序。

在那裏應該有一些你的WCF服務的配置 - 低於<system.serviceModel>的任何配置都可以。如果沒有 - 將必要的信息添加到您的配置!

好吧,所以如果你想在代碼中指定你的端點URL,你需要在實例化你的客戶端代理類的時候這麼做 - 否則,它會在config中查找。使用此代碼段,則會使用HTTP綁定的app.config中的配置設置,並分別指定的URL,在代碼:

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

MyService serviceproxy = new MyService(binding, address); 

這樣一來,basicHttpBinding的對象將從下的config讀取設置與name=MyServiceBinding的綁定。

+0

我用我的app.config的內容更新了我的問題。但請注意,我計劃在代碼中指定服務url,而不是在配置中,因爲url是用戶輸入。 – codymanix 2010-03-23 10:20:23

+0

這樣你完全忽略了配置文件中的內容。它仍然不知道合同。 – albertjan 2010-03-23 10:41:23

+0

您仍然必須指定合約。即使在代碼中,如果你想。他只說他想用代碼指定uri。 – albertjan 2010-03-23 12:15:43

1

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

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