使用 Http SOAP 使用自托管 WCF
Consuming Self Hosted WCF With Http 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;
}
}
}
我使用以下代码托管在应用程序中:
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();
}
}
}
}
和应用程序配置:
<?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 消息满怀希望地调用此服务
但我无法让它工作到现在!
所以我需要你关于可以使用 SOAP 消息调用上述托管服务的 c# 代码的帮助。
非常感谢您的努力。
我的客户端应用程序代码是:
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);
请问,我使用以下代码制作了一个简单的 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;
}
}
}
我使用以下代码托管在应用程序中:
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();
}
}
}
}
和应用程序配置:
<?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 消息满怀希望地调用此服务 但我无法让它工作到现在! 所以我需要你关于可以使用 SOAP 消息调用上述托管服务的 c# 代码的帮助。 非常感谢您的努力。
我的客户端应用程序代码是:
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);