3
votes

We have a requirement to call a WCF service from another WCF Service. To test this I build a sample console application to display a simple string. The setup is: Console App -> WCF Service 1 -> WCF Service 2 Console App calls a method of service 1 and the service 1 method eventually calls service 2 method to return a string. I am able to call Console -> Service 1 but Service 1 -> Service 2 is not working. It throws an exception: "Could not find default endpoint element that references contract 'ITestService2' 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." To accomplish this, I have created a WCF Service 2 with one method that returns a string (nothing fancy).

namespace TestServices
{
    [ServiceContract]
    public interface ITestService2
    {
        [OperationContract]
        string GetSomething(string s);
    }
}

Then I create service1 - ITestService1.cs and TestService1.cs that consumes service2 method GetSomething().

namespace TestServices
{
    [ServiceContract]
    public interface ITestService1
    {
        [OperationContract]
        string GetMessage(string s);
    }        
}

namespace TestServices
{
    class TestService1 : ITestService1
    {
        public string GetMessage(string s)
        {
            TestService2 client = new TestService2();
            return client.GetSomething("WELCOME " + s);
        }
    }
}

Note: I create a proxy for Service2 using svcutil.exe. It creates a app.config and TestService2.cs files that I copied in TestService1 project folder to reference.

Finally, I created the console app that just creates an instance of Service1 and calls the GetMessage() method.

static void Main(string[] args)
{
    TestService1 client = new TestService1();
    Console.WriteLine(client.GetMessage("Roger Harper"));
    Console.ReadKey();
}

When I call the service 2 directly from Console application, it works without any issue. The same config and proxy class when copied with in service 1. It throws error. The config file looks like: config file for service 1 in console application:

<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ITestService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:3227/WCFTestSite/TestService1.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITestService1"
                contract="ITestService1" name="WSHttpBinding_ITestService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

config file for service 2 in service1 folder:

<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ITestService2" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:3227/WCFTestSite/TestService2.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISriWCFTestService2"
                contract="ITestService2" name="WSHttpBinding_ITestService2">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

Appreciate if someone can help me in resolving this issue. I also tried prefixing the contract name with namespace but it didn't work. Not sure how the same config/proxy works directly from console and not with in another service. Please HELP!!! Thanks in advance.

1

1 Answers

1
votes

From my understating you have a console app that is self hosting a wcf service that service is calling a second wcf service. I am guessing you have a wcf service1 defined in dll that the console app loads and then attempts to call. I think your issue may be that sice service 1 is in a dll its not loading the config file where you have defined the link to service 2. Try creating the endpoint programmaticly and see if that gets you thorough the issue.