WCF implements multiple services, and wcf implements multiple
This article uses WCF to implement two services. One uses HTTP Communication and the other uses TCP communication.
The general idea is:
→ Create a WCF Service and interface, and write two interfaces
→ Implement two interfaces
→ Create a console project host for WCF, configure App. config, and enable host
→ Create a Web Client and reference the WCF Service
→ On the Web Client, call the WCF Service, that is, call the instance method of the WCF proxy class
Open Visual Studio 2013 and create a class library project.
Create a WCF Service named "HelloServie" under the class library. Then, components related to WCF are added to the project, and the IHelloServie and HelloServie classes are automatically created.
The IHelloService interface is modified as follows:
namespace MyServices
{
[ServiceContract]
public interface IOneService
{
[OperationContract]
string GetOne();
}
[ServiceContract]
public interface ITwoService
{
[OperationContract]
string GetTwo();
}
}
The HelloService class now needs to implement the above two interfaces at the same time.
namespace MyServices
{
// Note: You can use the "RENAME" command on the "refactoring" menu to change the class name "HelloServie" in the Code and configuration file at the same time ".
public class HelloServie : IOneService, ITwoService
{
public string GetOne()
{
Return "this message is transmitted over HTTP ";
}
public string GetTwo()
{
Return "this message is transmitted over TCP ";
}
}
}
Now you need a host. Create a console project under the current solution.
Add a reference to the class library where the WCF is located and a reference to "System. ServiceModel.
Set the configuration file of the console project.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<services>
<! -- Name = "namespace name. Implementation class name" -->
<service name="MyServices.HelloServie" behaviorConfiguration="mexBehaviour">
<endpoint address="MyServices" binding="basicHttpBinding" contract="MyServices.IOneService">
</endpoint>
<endpoint address="MyServices" binding="netTcpBinding" contract="MyServices.ITwoService">
</endpoint>
<baseAddresses>
<add baseAddress="http://localhost:9090/"/>
<add baseAddress="net.tcp://localhost:6060/"/>
</baseAddresses>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Where,
● Bind behavior through the behaviorConfiguration attribute of the Service Node
● The format of the service node name attribute value is: "namespace name. Implementation class name"
● An endpoint generally contains three attributes: "abc". a represents address, B represents binding, and c Represents contract.
● The format of the contract attribute value in endpoint is: "namespace name. Interface Name"
The console project is as follows:
using System.ServiceModel;
......
namespace MyHost
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloServie)))
{
host.Open();
Console. WriteLine ("host enabled, enabling time;" + DateTime. Now );
Console.ReadKey();
}
}
}
}
Set the console project as the startup project and start the console project to enable the host.
Now you need a client. Open a new Visual Studio solution.
Create an empty website named "MyServiceClient.
Once a reference to the WCF Service is added, the system. serviceModel node is automatically added to the Web. config of the website.
Create a Web interface.
<div>
<Asp: Button ID = "Button1" runat = "server" Text = "get http Communication Information" OnClick = "button#click"/>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /><br />
<Asp: Button ID = "Button2" runat = "server" Text = "Get TCP communication information" OnClick = "Button2_Click"/>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
Write button events.
// HTTP Communication Information
protected void Button1_Click(object sender, EventArgs e)
{
MyServiceClient.MyService.OneServiceClient client = new MyService.OneServiceClient("BasicHttpBinding_IOneService");
Label1.Text = client.GetOne();
}
// TCP communication information
protected void Button2_Click(object sender, EventArgs e)
{
MyServiceClient.MyService.TwoServiceClient client = new MyService.TwoServiceClient("NetTcpBinding_ITwoService");
Label2.Text = client.GetTwo();
}
Above, when the OneServiceClient and TwoServiceClient instances are declared, the real parameter value passed by the constructor is the name attribute value of endpoint in Web. config.
Click the following two buttons on the page:
It can be seen that a WCF Service can implement multiple services.