C # WCF interfaces for creating service contracts, deployment, and client connections

Source: Internet
Author: User

The service contract describes the type (interface or Class) exposed to the external, the operations supported by the service, the message exchange pattern used, and the format of the message. Each WCF service must implement at least one service contract. You must use a service contract to reference the namespace System.ServiceModel. Servicecontractattributeoperationcontractattribute 1, first create a class library for ClassLibrary1, creating an interface Interface1. 2, add references and namespaces, SYSTEM.SERVICEMODEL3, in the interface code as follows: namespace ClassLibrary1
{//service contract
[ServiceContract]
public interface Interface1
{//Operation contract
[OperationContract]
String Hello ();
}
}4, create a form or console program and create a class HELLOCLASS.CS5, add references (inside the project) Classlibrary16,helloclass1.cs code as follows

Class HelloClass:ClassLibrary1.Interface1
{

public string Hello ()
{
Return "Hello wcf!";

}
}

Basically create a service. Deployment is required after creation. Generally divided into configuration file deployment and code deployment.

One, the three elements of the configuration file deployment service

A:address means where (also contains transmission information)

B:binding means what to do (match the way the address is transmitted)

What c:contract means to do (service contract)

<system. Servicemodel>

<services>

<service>

<endpoint/>/* Services and Endpoints */

</service>

</services>

<bindings>/* Binding (optional) */

<binding>

</binding>

</bindings>

<behaviors>/* Behavior (Optional) */

<behavior>

</behavior>

</behaviors>

</system. Servicemodel>

The address of the endpoint is represented by the EndpointAddress class, which contains a Uniform Resource Locator (URI) representing the service address, and most of the transmitted address URIs contain four parts. For example

The URI of "Http://www.sina.com.cn:3200/mathservice" has the following four parts:

– Scenario: http:

– Computer: www.sina.com.cn

– (optional) port: 3200

– Path:/mathservice

Continue to refine on the example above, open the App. config file <system.serviceModel>
<services>
<service name= "Consoleapplication1.helloclass" behaviorconfiguration= "Testbehavior" > <!--name is the class that implements the contract-- >
<baseAddresses>
<add baseaddress= "Http://localhost:8002/test" ></add><!--Base Address--
</baseAddresses>
<endpoint address= "" binding= "BasicHttpBinding" contract= "Classlibrary1.interface1" ></endpoint>
<!--have baseaddress base addresses, address nullable, binding type, HTTP protocol, contract as exposed protocol, service contract interface created--
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name= "Testbehavior" > <!--is consistent with the above behaviorconfiguration= "Testbehavior" and can be empty--
<servicemetadata httpgetenabled= "true"/> <!--Specifies whether to publish metadata to Http/get get
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> need to start service after ConsoleApplication1 add apps and Namespaces System.ServiceModel and the following code in Program.cs

ServiceHost host = null;

Host = new ServiceHost (typeof (Consoleapplication1.helloclass));
Host. Open ();
Console.WriteLine ("Service has been started! ");
Console.ReadLine ();

After running the code, you will see the relevant service information when you open the Http://localhost:8002/test in the browser. Second, the code deployment first creates a console program ConsoleApplication2, adds a class HelloClass.cs and a reference and a namespace, and the service interface adds a namespace ServiceHost host = null in Program.cs;
Host = new ServiceHost (typeof (Consoleapplication2.helloclass));
NetTcpBinding tcpbind = new nettcpbinding ();//Set Binding type
string address = "Net.tcp://localhost:3200/hello";
Host. AddServiceEndpoint (typeof (Classlibrary1.interface1), tcpbind, address);//Add in service endpoint, protocol, binding type, endpoint address
Host. Opened + = delegate {Console.WriteLine ("Service started! "); Console.ReadLine (); };
Host. Open (); When a service is deployed successfully, the client can connect to the service and invoke the method, typically in one of two ways, as in the previous section, directly add a service reference to implement two, the pure Code implementation first creates a client, the clients console program and related references, namespaces, service interface references

Binding form
NetTcpBinding bind = new nettcpbinding ();
Provide the address where the customer service is connected
EndpointAddress address = new EndpointAddress ("Net.tcp://localhost:3200/hello");
Client Clearance channel Factory sends messages to service endpoints of different configurations
Channelfactory<classlibrary1.interface1> factory = new Channelfactory<classlibrary1.interface1> (BIND, address);
Gets the specified type through the Channel factory object
Classlibrary1.interface1 MyObject = Factory. CreateChannel ();
string s = MyObject. Hello ();
Console.WriteLine (s);
Console.ReadLine ();

Start the service first and run the client.

C # WCF interfaces for creating service contracts, deployment, and client connections

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.