使用Http SOAP使用自托管WCF

时间:2015-07-30 22:39:43

标签: c# wcf soap

请注意,我使用以下代码制作了一个简单的WCF自控制台托管服务:

using System.ServiceModel;
namespace SimpleService
{
    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        int IncrementNumber();
    }
}

实施:

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;

namespace SimpleService

{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
    public class SimpleService : ISimpleService
    {
        int _number;
        public int IncrementNumber()
        {
            _number = _number + 1;
            Thread.Sleep(100);
            Console.WriteLine(_number.ToString());
            return _number;
        }
    }
}

我在app中托管了以下代码:

using System;
using System.ServiceModel;

namespace Host
{
    class Program
    {
        public static void Main()
        {
            using (ServiceHost host = new
                ServiceHost(typeof(SimpleService.SimpleService)))
            {
                host.Open();
                Console.WriteLine("Host started @ " + DateTime.Now.ToString());
                Console.ReadLine();
            }
        }
    }
}

和app config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehavior" name="SimpleService.SimpleService">
        <endpoint address="SimpleService" binding="basicHttpBinding"
                  contract="SimpleService.ISimpleService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

现在iam尝试从Android移动应用程序调用此服务,该服务将通过此地址(http://amyamyamy.no-ip.org/)使用SOAP消息快速调用此服务 但我直到现在才能让它工作! 所以我需要你的c#代码的帮助,可以使用SOAP消息调用上述托管服务。 非常感谢您的努力。

我的客户端应用代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Xml;

namespace Client
{
    class Program
    {
        public static void Main()
        {
            //SimpleService.SimpleServiceClient client =
            //new SimpleService.SimpleServiceClient();
            var client = new WebClient();
            string data = GetSoap();
            Console.WriteLine(data);
  client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
        client.Headers.Add("SOAPAction", "\"http://tempuri.org/IService1/GetData\"");
        var response = client.UploadString("http://amyamyamy.no-ip.org/SimpleService/SimpleService.svc", data);
        Console.WriteLine("Number after first call = " +response.ToString());
        Console.ReadLine();
    }
        static string GetSoap()
        {
            string xmlPayloadText = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:GetData>
         <!--Optional:-->
         <tem:value></tem:value>
      </tem:GetData>
   </soapenv:Body>
</soapenv:Envelope>";
            return xmlPayloadText;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用~表示它会给您404错误,据我所知,这是因为服务配置文件中的service not found部分

<baseAddresses>

您实际上正在使用 <host> <baseAddresses> <add baseAddress="http://localhost:8080" /> </baseAddresses> </host> ,您应该使用正确的服务器名称或IP地址。

同样,在您的客户端应用程序中,您正尝试使用

获取服务元数据localhost
WSDL

对我而言,好像您的服务部署在IIS服务器中但不是,因此您无法访问该服务。

您应该更改基本地址,例如

http://amyamyamy.no-ip.org/SimpleService/SimpleService.svc

在您的客户端应用程序中尝试访问它,如

   <host>
      <baseAddresses>
        <add baseAddress="http://my_actual_server_name:8080/" />
      </baseAddresses>
    </host>

即您的客户端代码

http://my_actual_server_name:8080/SimpleService