简体   繁体   中英

C# WCF - net.tcp cannot find endpoint

I'm getting the following error message when trying to implement net.tcp WCF in C#:

"Could not find default endpoint element that references contract 'EventInterfaceService.IEventInterface' 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 contract could be found in the client element."

On my client side I have the following code:

    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();
    }

As you can see I get the EndPointAddress as sInterfaceUrl and the UserParticipantName as sUserParticipantName.

For the app.config I have the following:

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

When running this code in a stand-alone client (not the actual application) it works. I can't seem to find out what's wrong. Any tips would be great!

Thanks.

Edit: Is there any way configuring this purely at runtime, so I won't need the app.config? Regarding the comments below, config file may not be found or wrong one is being used.

Your comment mentioned that you are putting your app.config stuff into a class library. This won't be read. The app.config of the executing assembly gets read instead (or the web.config since this is ASP). You will need to add your relevant config info to the config file of the executing assembly (your ASP project).

Alternatively, you could use the static ConfigurationManager class to read in your specific app.config settings: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM