简体   繁体   English

c#客户端调用Java axis2 Web服务,对象“重置”

[英]c# client calling java axis2 web service, object “resets”

I am very new to web service stuff so please be kind. 我是Web服务人员的新手,请保持友好。

I have written a simple POJO class, and deployed it on an axis2 server: 我编写了一个简单的POJO类,并将其部署在axis2服务器上:

public class Database {

    private Project project;

    public void login(){
        project = new Project();
        project.setDescription("Hello there");
        project.setName("To me");
    }

    public Project getProject(){
        return project;
    }

}

I call the service from ac# client: 我从ac#客户端调用该服务:

localhost.Database db = new WindowsFormsApplication1.localhost.Database();
db.login();

localhost.getProjectResponse pr = new WindowsFormsApplication1.localhost.getProjectResponse();

pr = db.getProject();

When I debug the response is null. 当我调试时,响应为null。 At the java end, when I call getProject, the project object is null. 在Java端,当我调用getProject时,项目对象为null。

What's happening? 发生了什么? How do I preserve the state of project between service calls? 如何在两次服务调用之间保留项目状态?

I'm not sure why @shivaspk left a comment instead of writing an answer, it is quite correct: web service calls (not just axis calls) are meant to be stateless, so although the project object gets created by 我不确定为什么@shivaspk留下评论而不是写答案,这是完全正确的:Web服务调用(不仅是轴调用)本来是无状态的,所以尽管项目对象是由创建的

db.login();

when you call 你打电话时

db.getProject();

It is being called on a different instance of your Database class that was created by Axis to service the second call. 在Axis创建的数据库类的另一个实例上调用它,以服务于第二个调用。

There is no really good answer to your question, except for you to rethink what you are trying to do. 除了重新考虑自己想做的事情之外,您的问题没有一个很好的答案。 If you need some kind of authentication (via login), then that authentication needs to be part of every web service call. 如果您需要某种身份验证(通过登录),则该身份验证需要成为每个Web服务调用的一部分。

For most toolkits, web services are stateless by default. 对于大多数工具包,Web服务默认情况下是无状态的。 I think axis is no different. 我认为轴没有什么不同。

If you want to maintain state between calls then you will need to enable sessions. 如果要保持通话之间的状态,则需要启用会话。 An example on how to maintain sessions in axis can be found at: 有关如何在axis中维护会话的示例,请参见:

http://kickjava.com/src/test/session/TestSimpleSession.java.htm http://kickjava.com/src/test/session/TestSimpleSession.java.htm

On the .NET side you will need to assign a CookieContainer to your request to store the session identifier. 在.NET端,您需要为您的请求分配一个CookieContainer来存储会话标识符。 See HOW TO: Use CookieContainer to Maintain a State in Web Services for more information. 有关更多信息,请参见如何:使用CookieContainer维护Web服务中的状态

I think your code would look something like this: 我认为您的代码应如下所示:

localhost.Database db = new WindowsFormsApplication1.localhost.Database();
// Assign the CookieContainer to the proxy class.  
db.CookieContainer = new System.Net.CookieContainer();

db.login();

localhost.getProjectResponse pr = new WindowsFormsApplication1.localhost.getProjectResponse();
pr.CookieContainer = db.CookieContainer;

pr = db.getProject();


I think that should let you do what you want -- but I wouldn't recommend it . 我认为那应该让您做自己想做的事- 但我不建议这样做

Designing service interfaces is a bit different than designing object oriented interfaces. 设计服务接口与设计面向对象的接口有些不同。 Service interfaces typically eschew the use of state and instead require the consumer to provide all of the relevant information in the request. 服务接口通常避免使用状态,而是要求使用者在请求中提供所有相关信息。

From Service-Oriented Architecture : 面向服务的体系结构

Services should be independent, self-contained requests, which do not require information or state from one request to another when implemented. 服务应该是独立的,独立的请求,在实现时,它们不需要信息或从一个请求到另一个请求的状态。

I would definitely recommend reading that article and perhaps revisiting your design. 我绝对会建议您阅读该文章,并可能重新访问您的设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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