在未找到客户端端点时使用WCF服务时出错

时间:2016-06-10 12:23:25

标签: wcf wcf-client wcf-hosting

我正在研究ASP.NET WCF简单的HelloWorld示例。我已成功完成服务器端,但在客户端工作时遇到问题。我已经使用SVCUTIL.exe为我生成代理类。

在调试时我收到以下错误;

An exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll but was not handled in user code

Additional information: Could not find endpoint element with name    'WSHttpBinding_IHelloWorldService' and contract 'IHelloWorldService' in the  ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.

另一件事,如果我不能从服务器访问dll文件,我可以使用Channel Factory,比如说如果我有权访问WSDL url链接

在客户端app.config

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IHelloWorldService" />
            <binding name="WSHttpBinding_IHelloWorldServiceAsyn" />
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:8087/CreditUnionServices/HelloWorldServices/HelloWorldService"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloWorldService"
            contract="IHelloWorldService" name="WSHttpBinding_IHelloWorldService">
            <identity>
                <userPrincipalName value="DESKTOP-G6LE8I4\Khurram Zahid" />
            </identity>
        </endpoint>
        <endpoint address="http://localhost:8087/CreditUnionServices/HelloWorldServices/HelloWorldServiceAsyn"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloWorldServiceAsyn"
            contract="IHelloWorldServiceAsyn" name="WSHttpBinding_IHelloWorldServiceAsyn">
            <identity>
                <userPrincipalName value="xyz\abc" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

客户端代理渠道工厂

 public class HelloWorldClient
{
    public string SendTestMessage(string name)
    {
        ChannelFactory<IHelloWorldService> _HelloWorldClientService = new ChannelFactory<IHelloWorldService>("WSHttpBinding_IHelloWorldService");

        IHelloWorldService _HelloWorldChannelService = _HelloWorldClientService.CreateChannel();

        var _returnMessage = _HelloWorldChannelService.GetMessage(name);

        ((IClientChannel)_HelloWorldChannelService).Close();

        return _returnMessage;
    }
}

服务器端配置文件

 <system.serviceModel>
<services>
  <service name="App.Services.Managers.HelloWorldManager" behaviorConfiguration="DefaultServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8087/CreditUnionServices/HelloWorldServices"/>
      </baseAddresses>
    </host>
    <endpoint address="HelloWorldService" binding="wsHttpBinding" contract="App.Services.Contracts.IHelloWorldService"></endpoint>
    <endpoint address="HelloWorldServiceAsyn" binding="wsHttpBinding" contract="App.Services.Contracts.IHelloWorldServiceAsyn"></endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultServiceBehavior">
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>

更新代码

public static class HelloWorldClient 
{
    public static string SendTestMessage(string name)
    {

        HelloWorldServiceClient _helloWorldService = new HelloWorldServiceClient("WSHttpBinding_IHelloWorldService");

        var _returnMessage =  _helloWorldService.GetMessage("mr kz ....");

        return _returnMessage;
    }
}

0 个答案:

没有答案
相关问题