C# JSON WCF在C中显示空白页#
我创建了一个控制台应用程序作为WCF 我的班级很简单C# JSON WCF在C中显示空白页#,c#,json,wcf,C#,Json,Wcf,我创建了一个控制台应用程序作为WCF 我的班级很简单 [ServiceContract()] public interface ISimpleService { [OperationContract] Person GetData(string id); } public class SimpleService:ISimpleService { [WebInvoke(Method = "GET", ResponseFormat = W
[ServiceContract()]
public interface ISimpleService
{
[OperationContract]
Person GetData(string id);
}
public class SimpleService:ISimpleService
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "data/{id}")]
public Person GetData(string id)
{
// lookup person with the requested id
return new Person()
{
Id = Convert.ToInt32(id),
Name = "Leo Messi"
};
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
我的班级简单服务
[ServiceContract()]
public interface ISimpleService
{
[OperationContract]
Person GetData(string id);
}
public class SimpleService:ISimpleService
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "data/{id}")]
public Person GetData(string id)
{
// lookup person with the requested id
return new Person()
{
Id = Convert.ToInt32(id),
Name = "Leo Messi"
};
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
我的课程
class Program
{
static void Main()
{
//Create a URI to serve as the base address
var httpUrl = new Uri("http://localhost:8090/MyService/SimpleService");
//Create ServiceHost
var host = new ServiceHost(typeof(SimpleService), httpUrl);
//Add a service endpoint
host.AddServiceEndpoint(typeof(ISimpleService)
, new WSHttpBinding(), "");
//Enable metadata exchange
var smb = new ServiceMetadataBehavior
{
HttpGetEnabled = true
};
host.Description.Behaviors.Add(smb);
//Start the Service
host.Open();
Console.WriteLine("Service is host at " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
Console.WriteLine("Host is running... Press <Enter> key to stop");
Console.ReadLine();
}
类程序
{
静态void Main()
{
//创建一个URI作为基址
var httpUrl=新Uri(“http://localhost:8090/MyService/SimpleService");
//创建服务主机
var host=新服务主机(typeof(SimpleService),httpUrl);
//添加服务端点
host.AddServiceEndpoint(typeof(ISimpleService)
,新的WSHttpBinding(),“”);
//启用元数据交换
var smb=新的ServiceMetadataBehavior
{
HttpGetEnabled=true
};
host.Description.Behaviors.Add(smb);
//启动服务
host.Open();
WriteLine(“服务是位于”+DateTime.Now.ToString(CultureInfo.InvariantCulture)的主机);
Console.WriteLine(“主机正在运行…按键停止”);
Console.ReadLine();
}
我的问题是,当我转到时,它会显示一个空白页它没有运行此方法。如何解决此问题?您试图对RESTFUL服务使用
wsHttpBinding
,并且将此服务视为SOAP服务。请尝试改用webHttpBinding
。它会显示一条消息:由于En的地址筛选器不匹配,收件人无法处理带有“”的消息dpointDispatcher。检查发送方和接收方的端点地址是否一致。你能解决吗?我刚刚给了你一个正确的方向的指针-使用webHttpBinding
,而不是wsHttpBinding
webHttpBinding
是为RESTful WCF设计的,而wsHttpBinding
是为SOAP设计的。你在做REST,所以使用e正确的绑定。是的,我使用webHttpBinding
。但它会显示上面的消息。您是否在服务和客户端上都使用webHttpBinding
?请发布您的客户端代码。