简体   繁体   English

如何在客户端中调用WCF服务重载的构造函数

[英]How to call WCF Service overloaded constructors in client

I am new to WCF. 我是WCF的新手。 I created two overloaded constructor in WCF service. 我在WCF服务中创建了两个重载的构造函数。 I added reference of WCF Service in my client application. 我在客户端应用程序中添加了WCF服务的引用。 I can able to call the WCF methods from the client. 我可以从客户端调用WCF方法。 But I dont know, 但是我不知道

How to call the overloaded constructors from client? 如何从客户端调用重载的构造函数? Is it possible? 可能吗? .

I searched for the same, I looked some answers, but I am not able to understand the things clearly, as I new to this. 我搜索了相同的内容,看了一些答案,但是由于我对此并不陌生,所以我无法清楚地理解这些内容。 I am looking for straight forward simple example to call the WCF constructors. 我正在寻找直接简单的示例来调用WCF构造函数。

It will be helpful, if anyone provide a example link for reference. 如果有人提供示例链接以供参考,将很有帮助。

A SOAP service never exposes anything that it specific to the .NET platform. SOAP服务从不公开任何特定于.NET平台的内容。 That includes constructors. 这包括构造函数。

Keep in mind that anything exposed by a SOAP service is exposed by describing it in the WSDL. 请记住,SOAP服务公开的任何内容都可以通过在WSDL中进行描述来公开。 There is no way to describe in the WSDL: 在WSDL中无法描述:

  1. constructors 建设者
  2. events 事件
  3. indexers 索引
  4. generics 仿制药

etc. 等等

As I said, the services are instantiated server-side by the framework. 正如我所说,服务是在框架的服务器端实例化的。 You only have access to a generated proxy which under the hood connects to your service. 您只能访问在后台连接到您的服务的生成的代理。

Anyway, what you want is not doable via service constructors. 无论如何,您想要的内容无法通过服务构造函数来实现。 If you have a business object then pass that to a service operation: 如果您有业务对象,则将其传递给服务操作:

using(var serviceClient = new MyServiceClient())
{
   serviceClient.SomeOperation(businessObject);
}

The constructor you see here for MyServiceClient has absolutely no relation to the constructor you defined for the service contract. 您在此处为MyServiceClient看到的构造函数与您为服务合同定义的构造函数绝对没有关系。

Also, you may want to look into service instancing modes as it seems to me you don't want a per-call mode. 另外,您可能希望研究服务实例化模式,因为在我看来您不希望按呼叫模式。

you should be able to do it. 您应该能够做到。 A little understanding on how WCF instance managed is required. 需要对如何管理WCF实例有一点了解。

Theory : 理论:

  1. when a WCF service is invoked, a new instance is created per call by default (see InstanceContextMode) by the service dispatcher. 调用WCF服务时,服务分派器默认为每个调用创建一个新实例(请参阅InstanceContextMode)。

  2. a call to GetInstance() and ReleaseInstance() (of IInstanceProvider) is made to instantiate and release the service object respectively (except when InstanceContextMode is Single) depending up the InstanceContextMode . 取决于InstanceContextMode,分别调用了IInstanceProvider的GetInstance()和ReleaseInstance()来实例化和释放服务对象(除非InstanceContextMode为Single)。

  3. In above scenario, the default constructor is invoked. 在上述情况下,将调用默认构造函数。

  4. WCF provides extensibility point where you can inject your own instance provider (so that you'll be able to call the overloaded constructor). WCF提供了可扩展点,您可以在其中注入自己的实例提供程序(以便您可以调用重载的构造函数)。

Action : 动作:

  1. create a new Instance Provider by inheriting from IInstanceProvider. 通过从IInstanceProvider继承来创建新的Instance Provider。 Override GetInstance() and ReleaseInstance() methods. 重写GetInstance()和ReleaseInstance()方法。 on GetInstance() method use your overloaded constructore to return a new service instance. 在GetInstance()方法上,使用重载的构造函数可返回新的服务实例。

  2. create a new Service Behavior (say InstanceProviderBehavior) attribute (by inherting from Attribute and IServiceBehavior) to apply on service contract so that service dispatcher looks for your own custom Instance Provider. 创建一个新的服务行为(例如InstanceProviderBehavior)属性(通过从Attribute和IServiceBehavior继承)以应用于服务合同,以便服务调度程序查找您自己的自定义实例提供程序。

Here is a great article - http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx 这是一篇很棒的文章-http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM