简体   繁体   中英

How to pass additional data to a Web Service

From .NET(C#) code, we are invoking a Java Web Service. Its a SAOP request. The Java WebServices are developed using Axis 1.4.

Following is the sample code that makes the java web service request:

private string GetUserInfo(string strEID)
{
    string strEmpData = string.Empty;
    GetEmpInfo.EmpProxyService objEmp;           

    try
    {
    objEmp = new GetEmpInfo.EmpProxyService();
    strEmpData = objEmp.searchByEID(strEID);
    }
    catch (WebException ex)
    {

    }

    objEmp.Dispose();
    return strEmpData;
}

Now, we have a change request which requires passing some additional information - a name_value pair to the java webservice.

How can we achieve this ?

Can I pass the information in HTTP/SOAP headers?

Changing the method signature and adding the additional info to pass the info is not at all the good idea I guess.

EDIT: Its basically we want to add the logging inforamtion of who are all consuming the web services. Once the java webservice request is processed successfully, we will log the usage information along with the source of the request (from webappln/windows appln/flex client).

We want the clients to send its unique id to identify it. Since this has nothing to do with the business logic, can we add it meta-data info...say in headers.

If you have control over the service signature, I would actually suggest that you change the signature of this web service, or add another method that takes the additional arguments. When you're using a high-level language like C# or Java, the tendency is for the web service framework to abstract the entire SOAP stack away from you, and leaves you dealing with just the plain objects that eventually get serialized to make the method call. With only the argument objects exposed, it can be tricky to try to inject additional stuff into the SOAP message, if it's not part of the actual method signature.

There are usually ways to manipulate the SOAP message by hand, but I would probably shy away from that if possible, as editing the SOAP message by hand goes against the point of using a serialization-driven framework. That said, if you have no control over the service method, and the group in control of it needs you to pass additional data outside of the soap objects, you might be stuck messing with the SOAP message by hand.

If you want to add some future proofing to your services, I would suggest passing a full-fledged object rather than a single string or primitive value. In your object, you could include a key-value data store like a HashMap or Dictionary so that additional data can be passed without changing the signature or schema of the web service. With key-value data, documentation becomes important because it's no longer clearly specified data types or parameters.

You can use SOAP headers but I would rather not go that route since the headers have no business meaning. Rather change the signature and use request and response objects.

SearchByEIDResponse GetEmpInfo.EmpProxyService.searchByEID(SearchByEIDRequest)

Ths makes any changes less painfull and prevents huge parameter lists.

How you pass information to a web service is dependent on the methods that web service exposes. What language that service is written in is inconsequential to you as the consumer. IF the Java web service requires a name value pair to retrieve some data than a method signature will expose that.

objEmp.searchByEID(strEID, strVal1, strVal2);

That said you as Eben indicates you are better off using request and response objects to keep your parameter lists short. When to use these more complex types comes with experience ie don't use a request object from the get go if you need to only pass a single string value, but use a request object if you need to pass 50 string values.

如果您有多个Web服务,并且不想更改所有方法(这是合理的),则可以使用SoapExtension以http://msdn.microsoft.com/zh-cn/library/system.web.services.protocols .soapextension.aspx您在客户端上编写了肥皂扩展类,然后在web.config中对其进行了声明。

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