자체 호스팅 WCF RESTful (Json) 서버를 구축하려고합니다. 단계별 자습서에 따라 단계별로 수행하면 피들러에서 http://192.168.1.250:18688/MyService/GetJSON
요청 후 오류 400이 반환됩니다. 이 경우 SVC 파일의 수정이 필요하지만 실제로는 자체 호스팅 된 응용 프로그램에는 이러한 파일이 없습니다.WCF REST 자체 호스팅 반환 400 잘못된 요청
문제를 해결하는 방법은 무엇입니까? 고맙습니다!
인터페이스
namespace Contracts
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "Ping")]
bool Ping();
[OperationContract]
Dude GetDude();
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetJSON")]//,BodyStyle=WebMessageBodyStyle.Bare)]
string GetDudeJSON();
}
}
코드
namespace Contracts
{
[ServiceBehavior]
public class MyService:IMyService
{
public Dude Dummy { get; set; }
public MyService()
{
Dummy = new Dude("Dude", 28);
}
public bool Ping()
{
return true;
}
public Dude GetDude()
{
return Dummy;
}
string IMyService.GetDudeJSON()
{
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Dude));
ser.WriteObject(stream,Dummy);
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
// Console.WriteLine("Read:"+reader.ReadToEnd());
return reader.ReadToEnd();
}
}
}
의 app.config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="Contracts.MyService" behaviorConfiguration="MEXBehavior">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpBinding1" contract="Contracts.IMyService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://192.168.1.250:18688/MyService"/>
</baseAddresses>
</host>
</service>
</services>
<!-- A behavior definition for MEX -->
<behaviors>
<serviceBehaviors>
<behavior name="MEXBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="httpBinding1"></binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
CLI 호스트
static void Main(string[] args)
{
ServiceHost _host = new ServiceHost(typeof(Contracts.MyService));
_host.Open();
ChannelFactory<Contracts.IMyService> channel = new ChannelFactory<Contracts.IMyService>(
new BasicHttpBinding(),
new EndpointAddress("http://192.168.1.250:18688/MyService"));
Contracts.IMyService client = channel.CreateChannel();
while (true)
{
;
}
}
HTTP 요청 및 응답을 확인해야합니다. 응답자가 즉각적인 답을 제공하지 않으면 질문에 추가하십시오. –