简体   繁体   English

如何将wcf服务添加到现有的类库

[英]how to add wcf service to existing class library

I have a big MVC solution with multiple projects. 我有一个包含多个项目的大型MVC解决方案。 I am planning to create a separate solution with WCF services and move some highly resource hungry projects. 我打算用WCF服务创建一个单独的解决方案,并移动一些资源匮乏的项目。 Idea is that the MVC application will communicate with WCF for any computational requirements. 想法是MVC应用程序将与WCF通信以满足任何计算要求。

The problem is I dont know how do I call the existing class and interfaces which already have interfaces to services. 问题是我不知道如何调用已经具有服务接口的现有类和接口。 My class/interface: 我的班级/界面:

public interface IHelloWorld
{
    string SayHello(string name);
}

public class HelloWorld : IHelloWorld
{
    public string SayHello(string name)
    {
        return string.Format("Hello, {0}", name);
    }
}

There are 100s of methods in the class. 班上有100种方法。 Not all will be exposed to WCF only a few of them. 并非所有人都只接触到WCF中的一部分。

Now I have to call this class in newly created WCF service. 现在我必须在新创建的WCF服务中调用此类。 I am not sure: 我不确定:

  • Do I have modify the existing classes to convert to svc.cs (service) or I can create a separate service file and call the existing methods there? 我是否已修改现有类以转换为svc.cs(服务),或者我可以创建单独的服务文件并在那里调用现有方法?
  • The service class also needs an interface which will be defined in web.config (endpoint contract). 服务类还需要一个将在web.config(端点契约)中定义的接口。 Do I have to create a separate interface with only methods I need to expose OR I have to use the existing interface in the class library? 我是否必须创建一个单独的接口,只有我需要公开的方法或者我必须使用类库中的现有接口? If I use the class interface, then I have to mention the same in web.config. 如果我使用类接口,那么我必须在web.config中提到相同的内容。

I am bit confused what should be the best way to doing it. 我有点困惑应该是最好的方法。 I dont want to modify the existing class but want to add a new service which I can just hook up and call from there. 我不想修改现有的类,但想添加一个新的服务,我可以从那里连接和调用。 new svc.cs files are: 新的svc.cs文件是:

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract]
    string SayHello(string name);
}

public class HelloWorldService : IHelloWorldService
{
    private IHelloWorld helloWorld = new HelloWorld();
    public string SayHello(string name)
    {
        return helloWorld.SayHello(name);
    }
}

This current design seems I am repeating the existing class/interface. 这个当前的设计似乎在重复现有的类/接口。 Not sure if this is the correct way. 不确定这是否正确。 Please help. 请帮忙。

Do I have modify the existing classes to convert to svc.cs (service) or I can create a separate service file and call the existing methods there? 我是否已修改现有类以转换为svc.cs(服务),或者我可以创建单独的服务文件并在那里调用现有方法?

No, because you say that not all methods need to be exposed. 不,因为你说不是所有的方法都需要暴露。

This current design seems I am repeating the existing class/interface. 这个当前的设计似乎在重复现有的类/接口。 Not sure if this is the correct way. 不确定这是否正确。 Please help. 请帮忙。

Yes, it's repeating if you are doing like the code you show in the question. 是的,如果您像在问题中显示的代码一样,它会重复。 You can write the code of your service like this; 您可以像这样编写服务代码;

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract]
    string SayHello(string name);
}

public class HelloWorldService : IHelloWorldService
{
    private IHelloWorld helloWorld = new HelloWorld();

    public string SayHello(string name)
    {
        return helloWorld.SayHello(name);
    }
}

By using the IHelloWorld contract on your service, you are not duplicating logics and only needed method will be exposed through HelloWorldService contracts. 通过在服务上使用IHelloWorld合约,您不会复制逻辑,只需要通过HelloWorldService合约公开所需的方法。

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

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