Use WCF without referencing the service and manually write the client proxy class

Source: Internet
Author: User
Tags implement web services

Before I wrote a WCF development of the chat program, you can look at the previous blog.

In that chat program, I use WCF directly without referencing the service. Have not told you this knowledge before, for beginners friends, may not know how.

We say that WCF is much more powerful than a typical Web service because it is much more flexible than the average Web services, and not only can it run on an IIS server, but it can run in many ways, even a console application.

Now, you can recall the previous I wrote the "Legend of WCF", I most of the examples above are console application type. We should understand WCF as a communication technology, not just a service. In the previous example, I told you that when you complete the server side, you add a service reference to the client project, which generates the client proxy class, and we can use it as usual.

In fact, according to the way we talked earlier, it is enough to accomplish many practical tasks. Do you want to expand it? A friend will certainly ask: will it become difficult to expand? Rest assured that it will not be difficult, believe me, Old week will never talk about what we can not understand.

We might as well try it now, and instead of adding a service reference to the client, we write the proxy class that invokes the service ourselves. To do this, first of all, we want to be clear, in fact, we write a service contract, both in the server and the client need to use, if you have seen the addition of service references by the tool generated code, you will find that in fact it also generated in the Client service contract code. So, when we manually write the code that invokes the service, this is also required, so there are two ways to share a service contract between the server and the client, one is to copy the code to the client, another way, we can create a new class library, and then write the service contract into this class library, Finally, both the server side and the client reference this class library. For example, if there is an agreement that defines the following:

[ServiceContract]  
Public interface ITest  
{  
    [operationcontract]  
    int Add (int a, int b);  
      
    [OperationContract]  
    int Getrandmon ();  
      
    [OperationContract]  
    int Multiply (int a, int b);  
}

We then implement the contract on the server side, noting that the interface is implemented on the server side and not required by the client.

Implement service public  
class MyService:CommonLib.ITest  
{  
    Random m_rand = null;  
      
    Constructor public  
    MyService ()  
    {  
        M_rand = new Random ();  
    }  
      
    public int Add (int a, int b)  
    {return  
        a + b;  
    }  
      
    public int Getrandmon ()  
    {return  
        m_rand. Next ();  
    }  
      
    public int Multiply (int a, int b)  
    {return  
        a * b;  
    }  
}

Next, as before, create a service host and listen for client calls.

static void 

Main (string[] args)  
{  
    ServiceHost host = new ServiceHost (typeof (MyService));  
    HTTP mode  
    wshttpbinding httpbinding = new Wshttpbinding (securitymode.none);  
    Host. AddServiceEndpoint (typeof (Commonlib.itest), httpbinding, 

"http://localhost:8900/");  
    TCP mode  
    nettcpbinding tcpbinding = new NetTcpBinding (securitymode.none);  
    Host. AddServiceEndpoint (typeof (Commonlib.itest), tcpbinding, 

"net.tcp://localhost:1700/");  
    Open the service  
    host. Open ();  
    Console.WriteLine (the service is started.) ");  
    Console.read ();  
    Host. Close ();  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.