基于文章:https://www.cnblogs.com/songjianhui/p/7060698.html,增加了一个host实现多契约服务
一、在定义服务契约的类库项目Service.Interface 中添加新的服务接口IUser
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace Service.Interface
{
[ServiceContract(Name = "user")]
public interface IUser
{
[OperationContract]
string getuserid();
}
}
二、修改用于定义服务类型的类库项目Service的类CalculatorService, 使其同时继承ICalculator,IUser(不建议添加不同的Service实现不同的接口)
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service.Interface;
namespace Service
{
public class CalculatorService:ICalculator,IUser
{
public double add(double x,double y)
{
return x + y;
}
//实际按口IUser
public string getuserid()
{
return DateTime.Now.ToString("yyMMddHHmmssfff");
}
}
}
也可以用partial 定义CalculatorService在不同的文件里,分别继承ICalculator、IUser
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service.Interface;
namespace Service
{
public partial class CalculatorService:ICalculator
{
public double add(double x,double y)
{
return x + y;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service.Interface;
namespace Service
{
public partial class CalculatorService : IUser
{
public string getuserid()
{
return DateTime.Now.ToString("yyMMddHHmmssfff");
}
}
}
三、在Hosting添加新的服务对应的终结点(Endpoint)
修改配置文件,信息如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3721/calculatorservice/metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name="Service.CalculatorService">
<endpoint address="http://127.0.0.1:3721/calculatorservic" binding="wsHttpBinding" bindingConfiguration="" contract="Service.Interface.ICalculator"></endpoint>
<endpoint address="http://127.0.0.1:3721/user" binding="wsHttpBinding" bindingConfiguration="" contract="Service.Interface.IUser"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
四、修改ChannelClient对应的配置文件,添加新的终结点(Endpoint)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<client>
<endpoint name="calculatorService" address="http://127.0.0.1:3721/calculatorservic" binding="wsHttpBinding" contract="Service.Interface.ICalculator"/>
<endpoint name="user" address="http://127.0.0.1:3721/user" binding="wsHttpBinding" bindingConfiguration="" contract="Service.Interface.IUser"></endpoint>
</client>
</system.serviceModel>
</configuration>
就可以调用 IUser对应的方法了
using (ChannelFactory<IUser> userfactory = new ChannelFactory<IUser>("user"))
{
IUser proxy = userfactory.CreateChannel();
var c = proxy.getuserid();
Console.WriteLine(c);
Console.ReadLine();
}
下载地址:希望能给有需要的人带来一点点方便
https://pan.baidu.com/s/1mLqGREl1G5Cb_KRviXvycA
提取码: z2b2