C#WCF - net.tcp找不到端点

时间:2014-07-02 18:39:06

标签: c# .net wcf net.tcp

尝试在C#中实现net.tcp WCF时收到以下错误消息:

  

“无法找到引用合同'EventInterfaceService.IEventInterface'的默认端点元素   在ServiceModel客户端配置部分中。这可能是因为找不到您的应用程序的配置文件,   或者因为在客户端元素中找不到与此合同匹配的端点元素。“

在我的客户端,我有以下代码:

    private void Initialize(string sInterfaceUrl, string sUserParticipantName)
    {
        EventInterfaceCallbackSink _callbackSink;
        InstanceContext _instanceContext;
        EndpointAddressBuilder _endpointAddressBuilder;


        _callbackSink = new EventInterfaceCallbackSink();
        _instanceContext = new InstanceContext(_callbackSink);

        eventInterfaceClient = new EventInterfaceClient(_instanceContext); //Exception gets thrown here

        EndpointIdentity edi = EndpointIdentity.CreateUpnIdentity(sUserParticipantName);
        var endpointAddress = eventInterfaceClient.Endpoint.Address;

        EndpointAddressBuilder newEndpointAddress = new EndpointAddressBuilder(endpointAddress);
        newEndpointAddress.Uri = new Uri(sInterfaceUrl);
        newEndpointAddress.Identity = edi;

        eventInterfaceClient.Endpoint.Address = newEndpointAddress.ToEndpointAddress();
    }

如您所见,我将EndPointAddress作为sInterfaceUrl,将UserParticipantName作为sUserParticipantName。

对于app.config,我有以下内容:

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="basicHttpBinding_Interface" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="Infinite" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                textEncoding="utf-8" useDefaultWebProxy="true" messageEncoding="Text">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" />
                </security>
            </binding>
        </basicHttpBinding>
        <netTcpBinding>
            <binding name="NetTcpBinding_IEventInterface"/>             
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:8732/HTTPWCF/" binding="basicHttpBinding"
            bindingConfiguration="basicHttpBinding_Interface" contract="InterfaceService.IInterface"
            name="basicHttpBinding_Interface" />
        <endpoint address="net.tcp://localhost:8733/NETTCPWCF/"
            binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IEventInterface"
            contract="EventInterfaceService.IEventInterface" name="NetTcpBinding_IEventInterface">
        </endpoint>
    </client>
</system.serviceModel>

在独立客户端(而不是实际应用程序)中运行此代码时,它可以正常工作。我似乎无法找出什么是错的。任何提示都会很棒!

感谢。

编辑:有没有办法在运行时纯粹配置它,所以我不需要app.config?关于以下注释,可能找不到配置文件或使用了错误的配置文件。

1 个答案:

答案 0 :(得分:1)

您的评论提到您将app.config内容放入类库中。这将不会被阅读。执行程序集的app.config被读取(或者是web.config,因为这是ASP)。您需要将相关的配置信息添加到正在执行的程序集(您的ASP项目)的配置文件中。

或者,您可以使用静态ConfigurationManager类来读取特定的app.config设置: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx