使用Http SOAP消费自托管WCF
本文关键字:WCF Http SOAP 使用 | 更新日期: 2023-09-27 18:05:34
我做了一个简单的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配置:
<?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>
现在我试图从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;
}
}
}
对于404
,它意味着它给你一个service not found
错误,据我的理解,这是因为<baseAddresses>
部分在您的服务配置文件
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080" />
</baseAddresses>
</host>
您实际上正在使用localhost
,而您应该使用适当的服务器名称或IP地址。
WSDL
。http://amyamyamy.no-ip.org/SimpleService/SimpleService.svc
对我来说,这看起来像,如果你的服务部署在IIS服务器,但它不是,所以你无法访问服务。
你应该像
那样改变你的基址 <host>
<baseAddresses>
<add baseAddress="http://my_actual_server_name:8080/" />
</baseAddresses>
</host>
在您的客户端应用程序中尝试像
这样访问它http://my_actual_server_name:8080/SimpleService
也就是你的客户端代码
var response = client.UploadString("http://my_actual_server_name:8080/SimpleService", data);