Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6978417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:45:13+00:00 2026-05-27T17:45:13+00:00

While answering another question I bumped into this interesting situation Where WCF is happy

  • 0

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


    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:45:14+00:00Added an answer on May 27, 2026 at 5:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While answering this question I came across an interesting difference in the overload resolution
While answering another question, I thought of the following example: void *p; unsigned x
While answering this question regarding safe escaping of filename with spaces (and potentially other
While answering this question I noticed that I got the following dialog while atempting
In answering another question* on SO, and the subsequent comment discussion, I ran into
So, I was playing around with Python while answering this question , and I
What is the second line? (Seen while answering another question.) int * x =
While answering this question I noticed that I have never come across any property
While answering this question DOM parser: remove certain attributes only I noticed that some
Kicking around some small structures while answering this post , I came across the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.