简体   繁体   中英

Interesting WCF interface casting behaviour

While answering another question I bumped into this interesting situation Where WCF is happy to cast an interface with different number of members and from Different namespaces where normal .net runtime can't.

Can any one explain how WCF is able to do it and how to configure/force WCF to behave same as normal .net runtime. Please note that I know I should have only one interface and blah.. blah..

here is working code

using System;
using System.Runtime.Serialization;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace MyClient
{
  [ServiceContract]
  public interface IService
  {
    [OperationContract]
    string Method(string dd);
    [OperationContract]
    string Method2(string dd);
  }
}

namespace MyServer
{
  [ServiceContract]
  public interface IService
  {
    [OperationContract]
    string Method(string dd);
  }
}

namespace MySpace
{
  public class Service : MyServer.IService
  {
    public string Method(string dd)
    {
      dd = dd + " String from Server.";
      return dd;
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      string Url = "http://localhost:8000/";
      Binding binding = new BasicHttpBinding();
      ServiceHost host = new ServiceHost(typeof(Service));
      host.AddServiceEndpoint(typeof(MyServer.IService), binding, Url);
      host.AddDefaultEndpoints();
      host.Open();

      // Following line gives error as it should do. 
      //MyClient.IService iservice = (MyClient.IService)new MySpace.Service(); 

      // but WCF is happy to do it ;)
      ChannelFactory<MyClient.IService> fac = new ChannelFactory<MyClient.IService>(binding);
      fac.Open();
      MyClient.IService proxy = fac.CreateChannel(new EndpointAddress(Url));


      string d = proxy.Method("String from client.");
      fac.Close();
      host.Close();
      Console.WriteLine("Result after calling \n " + d);

      Console.ReadLine();


    }
  }
}

There is no inconsistency.

      // Following line gives error, as it should do, because the .NET types 
      // MyClient.IService and MySpace.Service are not related.   
      MyClient.IService iservice = (MyClient.IService)new MySpace.Service();   // ERROR !!

      // Likewise, a WCF client proxy defined using MyService.IService as the contract
      // cannot be cast to the unrelated .NET type MyClient.IService
      ChannelFactory<MyService.IService> fac1 = new ChannelFactory<MyService.IService>(binding); 
      fac1.Open(); 
      MyClient.IService proxy = (MyClient.IService)fac1.CreateChannel(new EndpointAddress(Url));  // ERROR !!

      // but the service can be consumed by any WCF client proxy for which the contract 
      // matches the defined service contract (i.e. they both expect the same XML infoset 
      // in the request and response messages). There is no dependency between the .NET type 
      // used in the client code and the .NET type used to implement the service. 
      ChannelFactory<MyClient.IService> fac = new ChannelFactory<MyClient.IService>(binding); 
      fac.Open(); 
      // Next line does not error because the ChannelFactory instance is explicitly 
      // specialised to return a MyClient.IService so the .NET type is the same... there is no cast
      MyClient.IService proxy = fac.CreateChannel(new EndpointAddress(Url)); 
      // NOTE: Thus far we have not done anything with the service in this case.
      // If we call Method() it should succeed, since the contract matches. If we call
      // Method2() the channel will fault as there is no matching operation contract in the service.

The .NET type system is a completely different concept to the WCF notion of service/operation/message/data contract. Just as well, otherwise you could never write a WCF client for a WCF service you didn't write yourself.

However, as the middle example shows, if you reuse the .NET type for the service contract in both service and client code, your expectation will be met.

Your MyClient.IService has the same method as MyServer.IService does WCF's channel factory thinks that the contract matches on the exposed url and hence processes the request.

Try changing your MyClient.IService method name and you can see it fail. Namespace are logical seperations as we know.

When you create a WCF Service and expose the wsdl it doesn't have any of your namespaces, unless you specify one in your configuration using bindingNamespace attribute in your endpoint element. Just try a sample and generate a proxy from the wsdl to see that the proxy doesn't have any namespace.

As long as the IService in your MyClient and MyServer namespace match your WCF code above would work

In regards to your code below:

MyClient.IService iservice = (MyClient.IService)new MySpace.Service();       

You are trying to cast MySpace.Service explicitly to MyClient.IService where your "Service" doesnt implement your MyClient.IService and is correct according to OOP. Since you have all the code in a single file and is self hosted might be giving you the confusion.

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