简体   繁体   English

从Silverlight调用Java Web服务会引发异常

[英]Calling a Java Web service from silverlight throws an exception

After my previous question HERE , I found the solution (well, part of it). 在我之前的问题HERE之后 ,我找到了解决方案(部分解决)。

Here's the code for Java part: 这是Java部分的代码:

@WebService

public class MyWebService { 公共类MyWebService {
@WebMethod public String myMethod() { @WebMethod public String myMethod(){
return "Hello World"; 返回“ Hello World”; } }

@WebMethod
public int Add(@WebParam(name="a") int a,
               @WebParam(name="b") int b)
{
    return a + b;
}


public static void main(String[] args) 
{       
    String address = "http://127.0.0.1:8023/_WebServiceDemo";
    Endpoint.publish(address, new MyWebService());
    System.out.println("Listening: " + address);
}

} }

And Here is the Silverlight part: 这是Silverlight的一部分:

private void SearchResultList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MyWebServiceClient proxy = new MyWebServiceClient();
        proxy.AddCompleted += proxy_AddCompleted;
        proxy.AddAsync(2, 3);
    }

    void proxy_AddCompleted(object sender, AddCompletedEventArgs e)
    {
        txtSearch.Text = e.Result.ToString();
    }

But when I run this, e.Result throws an exception. 但是,当我运行此命令时,e.Result会引发异常。 What am I missing/ How should I solve it? 我缺少什么/应该如何解决?

Note that this code runs perfectly in C# Console application (when it's not async). 请注意,此代码可以在C#控制台应用程序中完美运行(不异步时)。 But when I run the async code, it doesn't work. 但是,当我运行异步代码时,它不起作用。

Thanks in advance. 提前致谢。

I guess you are getting a System.ServiceModel.CommunicationException when trying to access the Java Webservice from Silverlight. 我想您在尝试从Silverlight访问Java Webservice时遇到了System.ServiceModel.CommunicationException。

There is nothing basically wrong with your code, and it should also work with async calls in C# Console App. 您的代码基本上没有任何问题,它也可以与C#Console App中的异步调用一起使用。

The main problem is that Silverlight (as a browser plugin) enforces some security restrictions that prevent a Silverlight Application to talk to another server than the one loaded from (defined by server name and port) without further configuration. 主要问题在于,Silverlight(作为浏览器插件)强制实施一些安全限制,以防止Silverlight应用程序与另一台服务器(而不是从其加载的服务器)通信(由服务器名称和端口定义),而无需进一步配置。 This behaviour can be configured as described here (also search for "silverlight cross-domain calls" or "silverlight cross domain policy"). 可以按照此处所述配置此行为(也可以搜索“ silverlight跨域调用”或“ silverlight跨域策略”)。

This restrictions (normally) do not apply to desktop or console applications so they'll work fine with the same web service. 该限制(通常)不适用于桌面或控制台应用程序,因此它们可以在同一Web服务上正常工作。

To make your code work you need to host the Silverlight Application inside the same "project" / website than your webservice (so I suppose, the self-hosting webservice won't work and you need to switch to Java web project where the webservice is to be hosted). 为了使您的代码正常工作,您需要将Silverlight应用程序托管在与Web服务相同的“项目” /网站中(因此,我想,自托管Web服务将无法正常工作,并且您需要切换到Java Web项目,该Web服务是托管)。 As the Silverlight Application basically consists of an enclosing HTML file plus the referenced binaries, you can host it on any server, eg Apache Tomcat. 由于Silverlight应用程序基本上由一个封闭的HTML文件以及引用的二进制文件组成,因此您可以将其托管在任何服务器上,例如Apache Tomcat。

Hope this helps. 希望这可以帮助。

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

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