[英]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“添加服务参考”功能。
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.