简体   繁体   English

如何使用C#和Visual Studio 2010在多个层之间共享服务引用类型

[英]How do I share Service Reference Types Across Multiple Tiers using C# and Visual Studio 2010

My project is split into a service tier project and an implementation tier project. 我的项目分为服务层项目和实施层项目。 Both are written in C# using Visual Studio 2010. The service tier accepts a request object and passes it to the implementation tier. 两者都是使用Visual Studio 2010用C#编写的。服务层接受请求对象,并将其传递给实现层。 The implementation tier manipulates this object and passes it to an external web service. 实现层操纵此对象,并将其传递给外部Web服务。 The result of the external web service is then passed back to a client via the implementation and service tiers. 然后,外部Web服务的结果通过实现和服务层传递回客户端。

The same request and response objects are used across each tier. 跨每个层使用相同的请求和响应对象。

Is there a way I can use a shared reference across both tiers, and use the same reference to communicate with the external web service? 有没有办法可以在两个层之间使用共享引用,并使用相同的引用与外部Web服务进行通信? If not, what would be the neatest approach? 如果没有,最整洁的方法是什么?

I'd prefer to avoid having to maintain project specific service references and instead use a single data contract dll. 我希望避免维护特定于项目的服务引用,而是使用单个数据协定dll。 When I use this approach however, I find I need to use Webservice.Request rather than DataContract.Request when communicating with the external web service method. 但是,当我使用这种方法时,我发现与外部Web服务方法进行通信时需要使用Webservice.Request而不是DataContract.Request。

One way would be to create a model that is shared by both your service and implementation tiers. 一种方法是创建一个由服务层和实现层共享的模型。 When you receive the response from a web service you can convert that object into your own model and pass it along. 当您收到来自Web服务的响应时,您可以将该对象转换为您自己的模型并将其传递。 When you need to update the data via web service convert your model back to the model requested by the web service. 当您需要通过Web服务更新数据时,请将您的模型转换回Web服务所请求的模型。

You could create a class like: 您可以创建一个类似的类:

public class MyCustomModel
{
    public int Id { get; set; }
    public string SomeValue { get; set; }
    // etc.
}

Then you can create extension methods that convert from WS to your Custom Model 然后,您可以创建从WS转换为自定义模型的扩展方法

public static class MyExtensions
{
    public static MyCustomModel ConvertToMyCustomModel(this MyCustomModel model, WebServiceModel wsModel)
    {
        var newModel = new MyCustomModel { Id = wsModel.Id, SomeValue = wsModel.SomeValue };

        return newModel;
    }
}

The same approach can be used to convert MyCustomModel to WebServiceModel. 可以使用相同的方法将MyCustomModel转换为WebServiceModel。 That way if you change your web service or if it points to another location you can easily update just one location rather than changing both service and implementation tier. 这样,如果您更改Web服务或它指向另一个位置,则可以轻松地仅更新一个位置,而不用同时更改服务层和实现层。

Not sure how you create proxies for your services. 不确定如何为服务创建代理。

Steps below are for Visual Studio "Add Service Reference" feature using. 以下步骤适用于Visual Studio“添加服务参考”功能。

  1. Move your contracts to dedicated project. 将合同移至专用项目。 Store only contacts without implementation there. 仅将联系人存储而不在此处实施。
  2. Add references to Contracts projects for Service and Client projects. 为服务和客户项目添加对合同项目的引用。
  3. Implement service in Service using contracts from Contracts and run service. 使用合同中的合同在服务中实施服务并运行服务。
  4. Add Service Reference in Service project. 在服务项目中添加服务参考。 Make sure that "Reuse Types in all referenced assemblies" is cheeked. 确保选中“所有引用的程序集中的重用类型”。

As result you should have classes defined in Contracts project used across whole solution. 结果,您应该在整个解决方案中使用的Contracts项目中定义了类。

Step 2 is critical here. 步骤2在这里至关重要。 Client needs to have reference to Contracts.dll otherwise it doesn't know about these types and will generate new types based on WSDL. 客户端需要引用Contracts.dll,否则它不了解这些类型,并将基于WSDL生成新的类型。

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

相关问题 如何在 Visual Studio 的 C# 中创建服务引用? - How do I create a Service Reference in C# in Visual Studio? 如何在Visual Studio 2010 C#中显示ApplicationRevision - How do I show the ApplicationRevision in Visual Studio 2010 C# 如何在Visual Studio 2010中使用C#创建简单的SOAP服务器? - How do I create a simple SOAP server using C# in Visual Studio 2010? 如何在Visual Studio中重命名c#类型? - How do I rename c# types in Visual Studio? 我可以在Visual Studio 2010(C#)中引用引用的库吗 - Can I reference a referenced library in Visual Studio 2010 (C#) 在1个解决方案中的多个项目之间共享资源。 Visual Studio 2010 - Share Resources Across Multiple Projects In 1 Solution. Visual Studio 2010 如何在Visual Studio 2010中引用C#类库项目? - How to reference a C# Class Library project in Visual Studio 2010? 如何在带有XNA的Visual Studio 2010中正确引用内容? - How do I properly reference Content in Visual Studio 2010 with XNA? 如何使用Visual Studio 2010将Windows控制台应用程序C#项目更改为Windows Service? - How to change Windows console app C# project into Windows Service using Visual Studio 2010? 如何使用C#和Visual Studio 2013从Treeview控件中一次选择多个节点 - How Do I Select Multiple Nodes At A Time From Treeview Control Using C# And Visual Studio 2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM