【发布时间】:2018-07-22 14:41:52
【问题描述】:
我在使用 Microsoft WCF 测试客户端对其进行测试并尝试调用其中一种方法时创建了上述 WCF 服务,响应为“ERROR 400 Bad Request”。 此外,服务的类型显示为 SOAP(检查图像)为什么?这是 WCF 的默认设置还是什么?
服务合同:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
[OperationContract]
string GetPatientNameByID(string ID);
[OperationContract]
PatientsEnt getClass();
[OperationContract]
[WebInvoke(UriTemplate ="AddNewBank",Method ="POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle =WebMessageBodyStyle.Wrapped,RequestFormat =WebMessageFormat.Json)]
bool AddNewBank(BanksEnt bank);
[OperationContract]
List<BanksEnt> GetBanksList();
// TODO: Add your service operations here
[OperationContract]
[WebGet(UriTemplate = "ValidateRel/RelationNumber={RelationNumber}/CallID={CallID}", ResponseFormat = WebMessageFormat.Json)]
string ValidateRel(string RelationNumber, string CallID);
[OperationContract]
[WebInvoke(Method = "POST")]
string testSer();
}
Service1.svc
public bool AddNewBank(BanksEnt ent)
{
try
{
return BanksBiz.AddNewBank(ent);
}
catch (Exception)
{
throw;
}
}
public List<BanksEnt> GetBanksList()
{
try
{
return BanksBiz.GetBanksList();
}
catch (Exception )
{
// File.AppendAllText("log.txt",ex.Message);
throw;// ex;
}
}
public string ValidateRel(string RelationNumber, string CallID)
{
return "hello";
}
public PatientsEnt getClass()
{
return null;
}
public string testSer()
{
return "test-1 service";
}
客户端中的端点配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="soapService" sendTimeout="00:05:00" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.example.net/cms/Service1.svc/soap"
binding="basicHttpBinding" bindingConfiguration="soapService"
contract="IService1" name="soapService" />
</client>
</system.serviceModel>
</configuration>
【问题讨论】:
-
端点是如何配置的,你设置正确的绑定和行为了吗?
-
请查看更新后的问题
-
您似乎指示它充当肥皂服务,那么您为什么希望它充当非肥皂服务?
-
您需要有一个带有 WebHttpBehavior 的 ServiceEndpoint。请阅读the docs。鉴于您对 wcf 有很多问题,您可能会考虑先学习一些基本教程,以掌握端点、绑定和契约背后的概念。
-
考虑不要透露您的端点地址,以保护 Web 服务免受 DoS 攻击
标签: c# rest web-services wcf soap