开发者

Custom template for Visual Studio Service References?

The Setup

I want to consume a web service in Visual Studio. I added a service reference, pointing it at a WSDL document. I get a bunch of generated code that works like a champ.

The Problem

In the Service Reference dialog, I selected "Internal" as the "Access level for generated classes." It appears as though that puts the "internal" keyword in front of the WhateverSoapClient class. Groovy. However, the generated model classes are all preceded by the "public partial" keywords. The "partial" part is fine (even desired), but not so the public. I want these classes to be internal as well.

The Solution?

Surely Visual Studio (svcutil.exe?) is making use of a template to generate Reference.cs, right? Is there any way to change that template, or specify an alternate? Or am I barking up the wrong tree?

Thanks!

I'm very new to this, so I may have phrased the question badly.

开发者_StackOverflow社区

Edit: In response to Tuzo's comment, I'm connecting Visual Studio to the following:

https://sebomarketing.worldsecuresystems.com/CatalystWebService/CatalystEcommerceWebservice.asmx?WSDL.

VS generates an internal class called CatalystEcommerceWebserviceSoapClient, which has a method called Catalogue_Retrieve(). Catalogue_Retrieve() returns a Catalogue object, and Visual Studio is generating the Catalogue class as "public partial". I want it to be internal.

Here are some snippets of what is getting generated:

internal partial class CatalystEcommerceWebsierviceSoapClient : System.ServiceModel.ClientBase<BcApi.EcommerceApi.CatalystEcommerceWebserviceSoap>, BcApi.EcommerceApi.CatalystEcommerceWebserviceSoap {
    public BcApi.EcommerceApi.Catalogue Catalogue_Retrieve(...) {
        // ...
    }
}

public partial class Catalogue : object, System.ComponentModel.INotifyPropertyChanged {
    // ...
}

And in response to Jamie, I'm familiar with T4 templates thanks to SubSonic. So I did already dig around a little bit for some a T4-related answer to this problem. I could probably dig a little more, though. Thanks for the tip.


You didn't include any of the attributes decorating the public class, but I'm guessing if you did, one of the attributes would have been:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="blahblahblah")]

If so, then the reason that svcutil is refusing to make these classes internal is due to the limitation of how the class data is being serialized; the XmlSerializer requires the classes to be public.

As such, the only thing that svcutil can make internal are the service interfaces and service implementation classes.

Even if you were to create a custom T4 template which generated all these classes as internal, you'd likely find that you were no longer able to communicate with the service.


This is not an answer to solve your problem, however, it may explain why you (and I) are seeing this in our data contract classes:

MSDN - Troubleshooting Service References


If you are not going to be Updating the Service Reference, you can edit this file manually. Yes, changes will be lost if you update, but this is not automatically updated, so you don't have to worry about this randomly happening.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜