c# - 将 Java 应用程序连接到 WCF Web 服务时出错

标签 c# java wcf eclipse java-metro-framework

我在 Eclipse 中编写了一个简单的 Java 控制台应用程序,它引用了用 C# 编写的 WCF Web 服务。在我的 PC 上本地托管 WCF 服务,我无法使用 java 客户端连接到该服务。

我创建 WCF 服务的步骤如下

  1. 使用以下端点创建“Service1.svc”:

    字符串 IService1.Hello(字符串 sName) { 返回“你好”+sName+“!” ; }

  2. 该服务的 Web 配置如下:

      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding"
            contract="WcfService1.IService1" name="BasicHttpEndpoint" />
        </client>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="true"/>
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    


  3. 我修改了属性,因此该服务将始终使用端口 8888。

  4. 我已使用 C# 控制台应用程序测试了该服务。

为了创建 Java 客户端,我执行了以下操作:

  1. 下载并安装与 Metro(Web 服务器)捆绑在一起的 Glassfish 3.0.1(应用程序服务器)

  2. 使用 jdk 的“bin”目录中的“wsimport”工具为 Java 客户端应用程序生成服务引用代码。我运行用于创建服务引用的 .bat 文件

  3. 将上述步骤 2 中的代码复制到 Eclipse 中的新 Java 应用程序中。

  4. 在我的控制台应用程序中创建一个新的 Java 类,该类调用 Web 服务,如下所示

`

import java.net.MalformedURLException;

import java.net.URL;

import javax.xml.namespace.QName;

import javax.xml.ws.BindingProvider;

import org.tempuri.Service1;

import org.tempuri.IService1;


public class Main {

/**
 * @param args
 */
public static void main(String[] args) {

    try {
        Service1 service = new Service1(new URL("http://localhost:8888/Service1.svc?wsdl"), new QName("http://tempuri.org", "Service1"));
        IService1 port = service.getBasicHttpBindingIService1();
        ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8888/Service1.svc");
        String sFileName = port.hello("Cal");

        System.out.println(sFileName);      


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        e.printStackTrace();
    }
}
}

`

当我尝试运行我的应用程序时遇到的错误如下:

Exception in thread "main" javax.xml.ws.WebServiceException: {http://tempuri.org}Service1 is not a valid service. Valid services are: {http://tempuri.org/}Service1
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:237)
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:182)
    at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:106)
    at javax.xml.ws.Service.(Unknown Source)
    at org.tempuri.Service1.(Service1.java:42)
    at Main.main(Main.java:17)
 

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

更改:

new QName("http://tempuri.org", "Service1")

至:

new QName("http://tempuri.org/", "Service1")

注意 org 后面额外的“/”。

关于c# - 将 Java 应用程序连接到 WCF Web 服务时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4451083/

相关文章:

c# - 使用 .Net Standard 2.0 库中的 x509 证书使用 WCF Web 服务?

c# - 层次结构中多类的 Fluent NHibernate 每个继承表 (TPH) 映射

c# - 使用 javascript 更改旧日期的文本颜色

C# SQL 查询滞后

asp.net - HMAC 和 WCF 服务 .net

.net - 禁止使用 TLS session 票证

c# - 如何使用 SoundPlayer 淡出 .wav 文件的音频而不是立即停止?

java - 如何在 SOLR 中以编程方式指定 update.chain

java - 在 Java 中如何获取数组、集合或字符串的大小?

java - java中的递归方法