读书人

WCF在win2003下是否可行

发布时间: 2013-02-06 14:02:21 作者: rapoo

WCF在win2003上是否可行?
本帖最后由 sola040515 于 2013-01-23 21:02:15 编辑 RT,在XP系统中开发的采用WCF的C/S架构工具,其中客户端是采用winform,服务器是使用webform;XP系统下运行没有问题,但是一将server部分放到服务器端,就会出现远程被积极拒绝的错误,客户端因而无法连接到服务器端。其中服务器端是win2003的系统,服务器端是采用IIS发布的。请问是否有什么解决办法。以下是WCF的部分代码:
Server端:


internal static ServiceHost myServiceHost = null;
public bool Open_service(string ip)
{
if (myServiceHost != null)
{
Stop_service();
}
if (ip != "")
{
Trace.Listeners.Clear();
Trace.Listeners.Add(new ConsoleTraceListener());
NetTcpBinding myBinding = new NetTcpBinding();
// WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.None;
myBinding.MaxReceivedMessageSize = 2147483647;
myBinding.TransferMode = TransferMode.Buffered;
myBinding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas();
myBinding.ReaderQuotas.MaxArrayLength = int.MaxValue;
myBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue;

Uri baseAddress = new Uri("net.tcp://" + ip + ":8057/WCFService/");
myServiceHost = new ServiceHost(typeof(Service1), baseAddress);
ServiceEndpoint myServiceEndpoint = myServiceHost.AddServiceEndpoint
(typeof(IService1), myBinding, "GetIdentity");
try
{
myServiceHost.Open();
if (myServiceHost.State == CommunicationState.Opened)


return true;
else
return false;
}
catch (Exception ex)
{
throw ex;
}
}
}
public void Stop_service()
{
myServiceHost.Close();
myServiceHost = null;
}


Client端:

static IService1 _proxy;

public static IService1 Proxy
{
get { return WCF._proxy; }
set { WCF._proxy = value; }
}
public static DuplexChannelFactory<IService1> DCF;
public static bool open_service(string ip, InstanceContext context)
{
if (ip != "")
{
try
{
ConsoleTraceListener ctl = new ConsoleTraceListener();
Trace.Listeners.Add(ctl);//添加管道监听
//通过TCP传输方式连接WCF
//设定WCF连接属性
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
binding.TransferMode = TransferMode.Buffered;


binding.MaxReceivedMessageSize = 2147483647;
binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas();
binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;

binding.OpenTimeout = new TimeSpan(0, 0, 100);
binding.ReceiveTimeout = new TimeSpan(0, 0, 100);
binding.SendTimeout = new TimeSpan(0, 0, 100);

//利用通道创建客户端代理
DCF = new DuplexChannelFactory<IService1>(context, binding, new EndpointAddress("net.tcp://" + ip + ":8057/WCFService/GetIdentity"));
_proxy = DCF.CreateChannel();
IContextChannel obj = _proxy as IContextChannel;//订阅消息
_proxy.RegisterListener();
try
{
_proxy.is_server_alive();
return true;
}
catch
{
return false;
}
}
catch
{
return false;

读书人网 >C#

热点推荐