tags:

views:

32

answers:

1

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.

A: 

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.

rerun
At first, I want to let you know that I am very new to WCF so my understanding would be limited as I am still reading and learning. I actually host service1 and service 2 locally on IIS. Then I create proxy classes for both along with the configuration files (app.config). These are created automatically with the help of scvutil.exe. Then I simply copy the service1.cs (proxy) and app.config into console app project. Now in the code I can access service1 methods by instansiating the service1 class. I didn't define any link between the two services explicitly in conifg. May be that is an issue.
I just create an instance in service1 to reference service2 methods. Is there an example to create the link in config and to programmatcaly create endpoints?
I think you are going to have to enable tracing on both service and use the tracutil tool to see what is happening.
rerun
rerun, I was able to resolve this as per your suggestion. I created the endpoint programmaticaly and it worked. But is there no way to do it otherwise?
I would step through your code and make sure you can resolve the settings in the config file it sounds like you are not getting thoes for some reason
rerun
How do I step through the code. VS doesn't allow me to get into the services.. thou I have all the projects in one solution.
take a look at this http://social.msdn.microsoft.com/forums/en-US/wcf/thread/8910c005-f7de-485e-88af-71589d00c070/ and since you say my sugestion work how about specifying my answer as correct . its kinda how so works
rerun
Oops.. sure I would love to do that :) and thanks for the help.