WCF服务端的契约层级关系如下:
[ServiceContract]
public interface IService1
{
[OperationContract]
int Add(int arg1, int arg2);
// 任务: 在此处添加服务操作
}
[ServiceContract]
public interface IService2 : IService1
{
[OperationContract]
int Mutil(int arg1, int arg2);
}
当客户端导入一个服务终结点的元数据时,如果该终结点的契约属于接口层级的一部分,则生成的客户端的契约将不再维持原理啊的层级关系。相反,它会取消层级,组成一个单独的契约,与终结点的契约名同名。这个单独的契约包含了层级中从上至下所有接口定义的操作,然而,如果使用OperationContract特性中的Action与ReplyAction属性,那么导入的接口定义仍然可以保留原来定义的每个操作的契约名。
对此,我们可以手动修改客户端代理及导入的契约定义,恢复层级关系。事例代码如下:
//------------------------------------------------------------------------------
//
// 此代码由工具生成。
// 运行库版本:2.0.50727.1891
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
//
//------------------------------------------------------------------------------
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IService1")]
public interface IService1
{
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Add", ReplyAction = "http://tempuri.org/IService1/AddResponse")]
int Add(int arg1, int arg2);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IService2")]
public interface IService2:IService1
{
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService2/Mutil", ReplyAction="http://tempuri.org/IService2/MutilResponse")]
int Mutil(int arg1, int arg2);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface IService2Channel : IService2, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase, IService1
{
public Service1Client()
{
}
public Service1Client(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public Service1Client(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public int Add(int arg1, int arg2)
{
return base.Channel.Add(arg1, arg2);
}
}
public partial class Service2Client : System.ServiceModel.ClientBase, IService2
{
public Service2Client()
{
}
public Service2Client(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public Service2Client(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public Service2Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public Service2Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public int Add(int arg1, int arg2)
{
return base.Channel.Add(arg1, arg2);
}
public int Mutil(int arg1, int arg2)
{
return base.Channel.Mutil(arg1, arg2);
}
}