Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 为什么我生成的客户机类没有';是否从ServiceContract接口继承?_.net_Wcf_Wcf Client - Fatal编程技术网

.net 为什么我生成的客户机类没有';是否从ServiceContract接口继承?

.net 为什么我生成的客户机类没有';是否从ServiceContract接口继承?,.net,wcf,wcf-client,.net,Wcf,Wcf Client,我最近开始使用WCF,并惊讶地发现由Add Service Reference生成的客户机类没有实现ServiceContract(我们称之为IMyInterface)的接口。相反,它实现了System.ServiceModel.ClientBase、IAnotherInterface,其中IAnInterface是一个类似于我的ServiceContract接口但实际上并不相同的接口。我看到一些类型是不同的,例如,它有数组而不是列表 这对我来说有点烦人,因为有时候我想远程调用web服务,有时候

我最近开始使用WCF,并惊讶地发现由Add Service Reference生成的客户机类没有实现
ServiceContract
(我们称之为
IMyInterface
)的接口。相反,它实现了
System.ServiceModel.ClientBase、IAnotherInterface
,其中
IAnInterface
是一个类似于我的ServiceContract接口但实际上并不相同的接口。我看到一些类型是不同的,例如,它有数组而不是列表

这对我来说有点烦人,因为有时候我想远程调用web服务,有时候我想使用本地对象,所以我希望它们实现相同的接口。目前,我正在将
ClientBase
对象包装到另一个对象中,这样它就实现了相同的接口,并将所有调用传递给
ClientBase
对象。这是推荐的方法还是有更简单的方法


我知道我可以在配置服务引用中将集合参数的类型从数组更改为列表或其他任何类型,但我怀疑这能否解决基本的类型问题。

我认为您希望将服务接口移动到单独的库(DLL/项目)中


然后有一个重载,允许您生成传入接口库(DLL)的客户端代理,以便它们不会重复。我认为标志是
-r
-reference
或类似的东西…

我认为您希望将服务接口移动到单独的库(DLL/项目)中


然后有一个重载,允许您生成传入接口库(DLL)的客户端代理,以便它们不会重复。我认为标志是
-r
-reference
或类似的东西…

您不需要使用添加服务引用或SvcUtil。您可以手动创建使用接口的客户端代理。您需要在web.config中使用一个绑定来使用默认构造函数,或者您可以传入端点和绑定

这种方法的一大优点是,您不必在每次更改接口和需要重新生成代理时都摆弄SvcUtil.exe。您将得到一个编译错误,而不是提醒您该类没有正确实现接口

SvcUtil和Add Service Reference主要用于您无权访问原始服务接口或合同的远程服务。该实用工具只读取服务wsdl并为您生成一个。因此,疯狂的命名可以确保它不会与项目中已有的内容冲突

此外,正如Reddog所提到的,将服务和数据契约移动到另一个项目/dll可以让您更容易地引用这些服务和数据契约

public partial class MyServiceClient : System.ServiceModel.ClientBase<MyProject.Contracts.IMyService>, MyProject.Contracts.IMyService
  {

    #region IMyService Members

    public void RecordSearchAnalytics(int a, int b, int c, string d, string e)
    {
      base.Channel.RecordSearchAnalytics(a, b, c, d, e);
    }

    #endregion

    #region boiler plate code 
    public MyHServiceClient()
    {
    }

    public MyHServiceClient(string endpointConfigurationName) :
      base(endpointConfigurationName)
    {
    }

    public MyHServiceClient(string endpointConfigurationName, string remoteAddress) :
      base(endpointConfigurationName, remoteAddress)
    {
    }

    public MyHServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
      base(endpointConfigurationName, remoteAddress)
    {
    }

    public MyHServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
      base(binding, remoteAddress)
    {
    }

    #endregion




  }
public分部类MyServiceClient:System.ServiceModel.ClientBase,MyProject.Contracts.IMyService
{
#区域IMyService成员
public void RecordSearchAnalytics(整数a、整数b、整数c、字符串d、字符串e)
{
基本渠道记录搜索分析(a、b、c、d、e);
}
#端区
#区域锅炉板代码
公共MyHServiceClient()
{
}
公共MyHServiceClient(字符串endpointConfigurationName):
基本(endpointConfigurationName)
{
}
公共MyHServiceClient(字符串endpointConfigurationName,字符串remoteAddress):
基本(endpointConfigurationName,remoteAddress)
{
}
公共MyHServiceClient(字符串endpointConfigurationName,System.ServiceModel.EndpointAddress remoteAddress):
基本(endpointConfigurationName,remoteAddress)
{
}
公共MyHServiceClient(System.ServiceModel.Channel.Binding绑定,System.ServiceModel.EndpointAddress remoteAddress):
基址(绑定、远程地址)
{
}
#端区
}

您不需要使用添加服务引用或SvcUtil。您可以手动创建使用接口的客户端代理。您需要在web.config中使用一个绑定来使用默认构造函数,或者您可以传入端点和绑定

这种方法的一大优点是,您不必在每次更改接口和需要重新生成代理时都摆弄SvcUtil.exe。您将得到一个编译错误,而不是提醒您该类没有正确实现接口

SvcUtil和Add Service Reference主要用于您无权访问原始服务接口或合同的远程服务。该实用工具只读取服务wsdl并为您生成一个。因此,疯狂的命名可以确保它不会与项目中已有的内容冲突

此外,正如Reddog所提到的,将服务和数据契约移动到另一个项目/dll可以让您更容易地引用这些服务和数据契约

public partial class MyServiceClient : System.ServiceModel.ClientBase<MyProject.Contracts.IMyService>, MyProject.Contracts.IMyService
  {

    #region IMyService Members

    public void RecordSearchAnalytics(int a, int b, int c, string d, string e)
    {
      base.Channel.RecordSearchAnalytics(a, b, c, d, e);
    }

    #endregion

    #region boiler plate code 
    public MyHServiceClient()
    {
    }

    public MyHServiceClient(string endpointConfigurationName) :
      base(endpointConfigurationName)
    {
    }

    public MyHServiceClient(string endpointConfigurationName, string remoteAddress) :
      base(endpointConfigurationName, remoteAddress)
    {
    }

    public MyHServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
      base(endpointConfigurationName, remoteAddress)
    {
    }

    public MyHServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
      base(binding, remoteAddress)
    {
    }

    #endregion




  }
public分部类MyServiceClient:System.ServiceModel.ClientBase,MyProject.Contracts.IMyService
{
#区域IMyService成员
public void RecordSearchAnalytics(整数a、整数b、整数c、字符串d、字符串e)
{
基本渠道记录搜索分析(a、b、c、d、e);
}
#端区
#区域锅炉板代码
公共MyHServiceClient()
{
}
公共MyHServiceClient(字符串endpointConfigurationName):
基本(endpointConfigurationName)
{
}
公共MyHServiceClient(字符串endpointConfigurationName,字符串remoteAddress):
基本(endpointConfigurationName,remoteAddress)
{
}
公共MyHServiceClient(字符串endpointConfigurationName,System.ServiceModel.EndpointAddress remoteAddress):
基本(endpointConfigurationName,remoteAddress)
{
}
公共MyHServiceClient(System.ServiceModel.Channel.Binding绑定,System.ServiceModel.EndpointAddress remoteAddress):
基址(绑定、远程地址)
{
}
#端区
}

WCF的默认行为正是您想要的