简体   繁体   中英

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. The implementation tier manipulates this object and passes it to an external web service. The result of the external web service is then passed back to a client via the implementation and service tiers.

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? 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. 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.

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. When you need to update the data via web service convert your model back to the model requested by the web service.

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

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. 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.

Not sure how you create proxies for your services.

Steps below are for Visual Studio "Add Service Reference" feature using.

  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.

Step 2 is critical here. Client needs to have reference to Contracts.dll otherwise it doesn't know about these types and will generate new types based on WSDL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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