简体   繁体   中英

Can't connect to WCF service from client

i get this error while trying to access a WCF service hosted in iis (locally) from ac# client:

Could not find endpoint element with name 'X' and contract 'Contract Name' 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.

This is the client's app.config (it is a NUnit fixture just to test connection):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_MyWs" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Transport">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
           <endpoint address="https://localhost/MyWs/MyWs.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_MyWs"
                    contract="MyServiceSvc.IMyWs"
                    name="BasicHttpBinding_MyWs" />
        </client>
   </system.serviceModel>
</configuration>

If i browse

https://localhost/MyWs/MyWs.svc?wsdl

i get correct output so the server part is working fine.
Moreover, i can test my ws with soapUI so the problem must be in the client, maybe in the app.config.

My client code, just in case someone wants tolook at it:

    [TestFixture]
    public class MyWsProxyFixture
    {
        private readonly MyWsClient _client = new MyWsClient();

        [Test]
        public void ProxyCreation()
        {
            Assert.IsNotNull(_client);
        }

        //More tests here
}

Even funnier, the exact same configuration and client works in another machine. Yes it is true. I cannot believe it myself but so it is.

Can you give some hints or suggestions to work this out?
Thanks!

PS: the service reference in the client is fine.

As far as I see it can be a multiple projects config problem:

If you have multiple projects in your solution then you have to put that configuration file in the config of the particular project that is consuming wcf service.

Suppose you have 2 projects then there will be 2 corresponding config files.

It came out that the problem was not in the App.config at all.
In fact, the very same App.config was working right in another machine.
The problem was with Nunit. By default, it uses a "Shadow copy" setting that copyes all dlls in a temp folder and executes the binary from there. What i cannot understan is why it does copy dlls but not .config. It just make no sense!
After i set shadow copy to false it started working in my machine too.

PS: The client wa working int other machine because the environment was running under Resharper, which turns off by default the shadow copy.
It makes sense now!

Hope this helps someone with same stupid problem

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