我在这方面的专家,但也许你错过了绑定。这里是我可以创建和托管费在代码WCF服务的最简单的例子(您需要将引用添加到系统,System.Runtime.Serializaton和System.ServiceModel,但除此之外,这个代码完成)。
using System;
using System.Runtime.Serialization;
using System.ServiceModel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Create the host on a single class
using
( ServiceHost host
= new ServiceHost
( typeof(MyService)
, new Uri("http://localhost:1234/MyService/MyService")
)
){
// That single class could include multiple interfaces to
// different services, each must be added here
host.AddServiceEndpoint
( typeof(IMyService)
, new WSHttpBinding(SecurityMode.None)
// Each service can have it's own URL, but if blank use the
// default above
, ""
);
// Open the host so it can be consumed
host.Open();
// Consume the service (this cuold be in another executable)
using
( ChannelFactory<IMyService> channel
= new ChannelFactory<IMyService>
( new WSHttpBinding(SecurityMode.None)
, "http://localhost:1234/MyService/MyService"
)
){ IMyService myService = channel.CreateChannel();
Console.WriteLine(myService.GetValue());
}
// Clean up
host.Close();
}
}
}
[ServiceContract]
public interface IMyService
{ [OperationContract] int GetValue();
}
public class MyService : IMyService
{ public int GetValue()
{ return 5;
}
}
}
我在结合加入,但它冻结起来,变为不响应在等效“Console.WriteLine(myService.GetValue());” – ZoomVirus 2014-10-08 07:58:19
@ZoomVirus它会冻结我发送的示例代码吗?当它冻结在您的等效代码中时,处于无限冻结状态,或超时。你有什么例外吗?出 – Eliott 2014-10-08 08:41:24
这时候我跑在Visual Studio调试,但如果我点击等待它出从未回应它倍。你放的示例代码也是一样的。 – ZoomVirus 2014-10-08 08:46:15