开发者

ASMX Use compiled type instead of generated type for SoapHeader

I haven't worked much with soap headers, so I'm hoping there's an answer here. Here's a simple example of what I'm trying to accomplish.

I have an ASMX web service and a client, along with a shared DLL. In shared.dll, I have a serializable type, let's call it CustomHeader, deriving from SoapHeader. There's a web method that acc开发者_C百科epts this as input via a SoapHeader attribute, so my service looks like:

[WebService]
public class MyService : WebService {
    public CustomHeader MyCustomHeader { get; set; }

    [WebMethod]
    [SoapHeader("MyCustomHeader", Direction = SoapHeaderDirection.In)]
    public void Go() { }
}

So far, so good. Inside the Go() method, I can access the MyCustomHeader object and do things with it. From the client, when I generate a proxy, the generated code includes a CustomHeader class containing the data properties of the MyService object, and a property called CustomHeaderValue, which I can set on the client before making the service call to Go(), and it passes it along just fine.

The issue is that the original CustomHeader class had constructors and methods that helped to populate the data fields (hash functions, calculated values, etc.). Since the client has a reference to the shared library, the client can create an instance of the original CustomHeader class, but it can't use that object in the service call, since it's technically a different type.

I can think of a few ways of handling this:

1) Convert the CustomHeader object to the generated CustomHeader class by pulling the properties over one at a time. This wouldn't be much processing, but it would mean that I'd either need to use reflection to loop through the properties, or touch the conversion code whenever the CustomHeader class changes.

2) Serialize the CustomHeader object, then deserialize it into the generated CustomHeader class - since they're really identical aside from the constructors and methods, the serialization should work just fine. This would be the easiest way, but it requires an extra round of serializing/deserializing, which, while not terribly expensive, is still additional processing.

3) Modify the generated code to make the CustomHeaderValue property of my shared type instead of the generated type. My opinion is that this is a horrible way of doing things, but it probably would be the least expensive of these options. I'm not going to do this option, but I just wanted to put it out there since technically it will work.

Am I missing something? Is there an accepted pattern for doing this?

Thanks for the help.


ASMX web services do not support reuse of types between the client and the service.

WCF does support this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜