看到博主也看了这本书,所以如果有幸,希望博主能指导指导
我第一次学习WCF,参照第一章,第一个实例写了代码,可是host.Open()失败,弄了好久,还是不行,如果博主有空回我一下哦
//新建一个服务
Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace HelloIndigo
{
[ServiceContract(Namespace = "http://www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
public class HelloIndigoService : IHelloIndigoService
{
public string HelloIndigo() {
return "Hello Indigo";
}
}
}
Host
Program.cs
//创建一个宿主
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ServiceFromScratch
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("http://localhost:8000/HelloIndigo")))
{
host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new BasicHttpBinding(), "HelloIndigoService");
host.Open();
Console.WriteLine("Press <Enter> to terminate the service host");
Console.WriteLine();
}
}
}
}
F5运行时,提示跨域访问失败
HTTP could not register URL http://+:8000/HelloIndigo/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
看看你配置文件中的绑定类型是不是wsHttpbinding,如果是,把 host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new BasicHttpBinding(), "HelloIndigoService");中的BasicHttpBinding改成wsHttpbinding,当然也可以把配置文件中的wsHttpbinding改成basicHttpBinding,不过没有wsHttpbinding绑定传输的安全,记住第一个字母要小写。