问题描述
限时送ChatGPT账号..我正在尝试创建一个服务以通过 Internet 使用它,但由于某种原因我无法访问它,我需要帮助查找我所犯的错误.我把代码留给你看.
公共类 ServiceHost;: System.ServiceModel.ServiceHost{public ServiceHost(Type serviceType, params Uri[] baseAddresses):基地(服务类型,基地地址){}公共服务主机(对象单例实例,参数 Uri[] baseAddresses):base(singletonInstance, baseAddresses){}受保护的服务主机(): 根据(){}public void EnableMetadataExchange(bool enableHttpGet = true){if (State == CommunicationState.Opened){throw new InvalidOperationException(La comunicación ya está abierta");}服务元数据行为元数据行为= Description.Behaviors.Find();如果(元数据行为 == 空){metadataBehavior = new ServiceMetadataBehavior();Description.Behaviors.Add(metadataBehavior);if (BaseAddresses.Any(uri => uri.Scheme == "http"))metadataBehavior.HttpGetEnabled = enableHttpGet;}AddAllMexEndPoints();}public bool HasMexEndpoint{得到{返回 Description.Endpoints.Any(端点=>端点.Contract.ContractType ==typeof(IMetadataExchange));}}私有无效 AddAllMexEndPoints(){Debug.Assert(HasMexEndpoint == false);foreach(BaseAddresses 中的 Uri baseAddress){绑定绑定=空;开关(baseAddress.Scheme){案例net.tcp":{绑定 = MetadataExchangeBindings.CreateMexTcpBinding();休息;}案例http":{绑定 = MetadataExchangeBindings.CreateMexHttpBinding();休息;}案例https":{绑定 = MetadataExchangeBindings.CreateMexHttpsBinding();休息;}案例net.pipe":{绑定 = MetadataExchangeBindings.CreateMexNamedPipeBinding();休息;}}如果(绑定!= null){AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX");}}}}
托管
public void HostService(){尝试{Uri tcpBaseAddress = new Uri("net.tcp://192.168.1.110:28620/");Uri httpBaseAddress = new Uri("http://192.168.1.110:28621/");ServiceHosthost = new ServiceHost(typeof(wesling.Services.GC), tcpBaseAddress, httpBaseAddress);//添加tcp绑定var netTcpBinding = new NetTcpBinding(){MaxBufferPoolSize = int.MaxValue,MaxReceivedMessageSize = int.MaxValue,ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas(){MaxStringContentLength = int.MaxValue},};netTcpBinding.Security.Mode = SecurityMode.None;host.AddServiceEndpoint(typeof(wesling.Services.IGC),netTcpBinding,GC");//添加WSHttp绑定var httpBinding = new WSHttpBinding(){MaxBufferPoolSize = int.MaxValue,MaxReceivedMessageSize = int.MaxValue,ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas(){MaxStringContentLength = int.MaxValue},};httpBinding.Security.Mode = SecurityMode.None;host.AddServiceEndpoint(typeof(wesling.Services.IGC), httpBinding, GC");host.EnableMetadataExchange(true);主机.Open();}捕获(通信异常 ce){MessageBox.Show(ce.Message);}捕获(异常前){MessageBox.Show(ex.Message);}}
<块引用>
uri ip 是托管服务的 pc 的 lan ip
客户是这样的
public async void GetProduct(){尝试{var endPointConfiguration = "WSHttpBinding_IGC";//cfg.GetEndPointConfiguration();var address = "http://fabianwesling.dynu:28621/GC";//cfg.getAddress();ServiceReference1.GCClient service = new ServiceReference1.GCClient(endPointConfiguration, address);var bindins = service.Endpoint.Binding;如果(绑定是 NetTcpBinding tcpBinding){tcpBinding.MaxBufferPoolSize = int.MaxValue;tcpBinding.MaxReceivedMessageSize = int.MaxValue;tcpBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue;}else if (bindins 是 WSHttpBinding wS){wS.MaxBufferPoolSize = int.MaxValue;wS.MaxReceivedMessageSize = int.MaxValue;wS.ReaderQuotas.MaxStringContentLength = int.MaxValue;}var result = await service.GetProductsAsync();}捕获(异常前){MessageBox.Show(ex.Message);}}
<块引用>
我在 Reuter 中打开了端口,以便将这些端口定向到 pc,我也在防火墙中打开了端口,我还激活了 Windows 框架功能.但是当我尝试从客户端连接时,它告诉我没有结束监听一定有一些我不理解的概念,但我无法确定它是什么......我需要你的建议,欢迎一切
解决方案先尝试在客户端电脑上用浏览器访问http//fabianwesling.dynu:28621/GC,看是否可以正常访问元数据.否则,客户端和服务器无法正常访问.修改客户端电脑上的hots文件:
总而言之,您必须确保客户端和服务器可以在 Internet 上相互访问.
如果不是网络问题,可以创建一个简单的WCF服务进行测试,知道问题出在哪里.下面是一个非常简单的WCF服务示例:
Baseaddress 是 WAN IP.
确保本地计算机可以通过浏览器访问远程服务的WSDL
I am trying to create a service to consume it through the internet, but for some reason I cannot access it and I need help finding the error I am making. I leave the code for you to see.
public class ServiceHost<T> : System.ServiceModel.ServiceHost
{
public ServiceHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
}
public ServiceHost(object singletonInstance, params Uri[] baseAddresses)
:base(singletonInstance, baseAddresses)
{
}
protected ServiceHost()
: base()
{
}
public void EnableMetadataExchange(bool enableHttpGet = true)
{
if (State == CommunicationState.Opened)
{
throw new InvalidOperationException("La comunicación ya está abierta");
}
ServiceMetadataBehavior metadataBehavior
= Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
Description.Behaviors.Add(metadataBehavior);
if (BaseAddresses.Any(uri => uri.Scheme == "http"))
metadataBehavior.HttpGetEnabled = enableHttpGet;
}
AddAllMexEndPoints();
}
public bool HasMexEndpoint
{
get
{
return Description.Endpoints.Any(
endpoint => endpoint.Contract.ContractType ==
typeof(IMetadataExchange));
}
}
private void AddAllMexEndPoints()
{
Debug.Assert(HasMexEndpoint == false);
foreach (Uri baseAddress in BaseAddresses)
{
Binding binding = null;
switch (baseAddress.Scheme)
{
case "net.tcp":
{
binding = MetadataExchangeBindings.CreateMexTcpBinding();
break;
}
case "http":
{
binding = MetadataExchangeBindings.CreateMexHttpBinding();
break;
}
case "https":
{
binding = MetadataExchangeBindings.CreateMexHttpsBinding();
break;
}
case "net.pipe":
{
binding = MetadataExchangeBindings.CreateMexNamedPipeBinding();
break;
}
}
if (binding != null)
{
AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX");
}
}
}
}
Hosting
public void HostService()
{
try
{
Uri tcpBaseAddress = new Uri("net.tcp://192.168.1.110:28620/");
Uri httpBaseAddress = new Uri("http://192.168.1.110:28621/");
ServiceHost<wesling.Services.GC> host = new ServiceHost<wesling.Services.GC>(typeof(wesling.Services.GC), tcpBaseAddress, httpBaseAddress);
//add tcp binding
var netTcpBinding = new NetTcpBinding()
{
MaxBufferPoolSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxStringContentLength = int.MaxValue
},
};
netTcpBinding.Security.Mode = SecurityMode.None;
host.AddServiceEndpoint(typeof(wesling.Services.IGC), netTcpBinding, "GC");
//add WSHttp binding
var httpBinding = new WSHttpBinding()
{
MaxBufferPoolSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxStringContentLength = int.MaxValue
},
};
httpBinding.Security.Mode = SecurityMode.None;
host.AddServiceEndpoint(typeof(wesling.Services.IGC), httpBinding, "GC");
host.EnableMetadataExchange(true);
host.Open();
}
catch (CommunicationException ce)
{
MessageBox.Show(ce.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The uri ip is the lan ip of the pc where the service is hosted
The customer is like this
public async void GetProduct()
{
try
{
var endPointConfiguration = "WSHttpBinding_IGC";//cfg.GetEndPointConfiguration();
var address = "http://fabianwesling.dynu:28621/GC"; //cfg.getAddress();
ServiceReference1.GCClient service = new ServiceReference1.GCClient(endPointConfiguration, address);
var bindins = service.Endpoint.Binding;
if (bindins is NetTcpBinding tcpBinding)
{
tcpBinding.MaxBufferPoolSize = int.MaxValue;
tcpBinding.MaxReceivedMessageSize = int.MaxValue;
tcpBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
}
else if (bindins is WSHttpBinding wS)
{
wS.MaxBufferPoolSize = int.MaxValue;
wS.MaxReceivedMessageSize = int.MaxValue;
wS.ReaderQuotas.MaxStringContentLength = int.MaxValue;
}
var result = await service.GetProductsAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I opened the ports in the Reuter so that it directs those ports to the pc, and I also opened the ports in the firewall, I also activated the Windows framework features. But when I try to connect from the client, it tells me that there was no end listening There must be some concept that I am not understanding, but I cannot identify what it is ... I need your advice, everything is welcome
解决方案
First try to use the browser on the client computer to visit http//fabianwesling.dynu:28621/GC to see if the metadata can be accessed normally.If not, the client and server cannot access normally. Modify the hots file on the client computer:
All in all, you must ensure that the client and server are mutually accessible on the Internet.
If it is not a network problem, you can create a simple WCF service for testing to know where the problem is.Here is a very simple example of WCF service:
https://docs.microsoft/en-us/dotnet/framework/wcf/getting-started-tutorial
UPDATE
This is my remote WCF service:
Baseadress is the WAN IP.
Ensure that the local computer can access the WSDL of the remote service through a browser
这篇关于WCF 自托管从互联网访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论