如何在WCF中启用webHttp连接?
我正在开发一个将数据从 Android 手机传输到服务器的解决方案(用 C#/.NET 编写)。
我创建了一个 WCF 服务并使用模拟器进行测试,一切正常。 然后,当我尝试从手机(连接到家庭 WiFi 网络)登录时,我收到以下异常消息:
org.apache.http.conn.HttpHostConnectException:连接到 http://192.168.1.5:8000拒绝
我会如果有人可以查看配置文件和界面并就如何启用连接提供任何建议,我真的很感激。
web.config:
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="DefaultBinding"
allowCookies="true"
bypassProxyOnLocal="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="RESTFriendly">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="RESTServer.LoginService">
<endpoint address=""
behaviorConfiguration="RESTFriendly"
binding="webHttpBinding"
bindingConfiguration="DefaultBinding"
contract="RESTServer.ILoginService" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
</configuration>
接口:
[ServiceContract()]
public interface ILoginService
{
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/username={username}&password={password}")]
[OperationContract]
Message Login(string username, string password);
}
服务实现:
public class LoginService : ILoginService
{
public Message Login(string username, string password)
{
Message message = new Message();
SQLWorks sqlWorks = new SQLWorks();
string strSession = sqlWorks.Login(username, password);
string strMessage;
message.Session = strSession;
if(strSession == "")
{
strMessage = "Login failed! Please check your username/password!";
}
else
{
strMessage = "Login Successful";
}
message.ErrorMessage = strMessage;
return message;
}
}
I am developing a solution for transfering data from android phone to the server (written in C#/.NET).
I created a WCF service and testing with emulator everything worked fine. Then when I tried to login from mobile phone (connected to home wifi network) I got the following exception message:
org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.5:8000 refused
I would really appreciate if anyonecould give a look at the config file and the interface and give any advice on how to enable connection.
web.config:
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="DefaultBinding"
allowCookies="true"
bypassProxyOnLocal="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="RESTFriendly">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="RESTServer.LoginService">
<endpoint address=""
behaviorConfiguration="RESTFriendly"
binding="webHttpBinding"
bindingConfiguration="DefaultBinding"
contract="RESTServer.ILoginService" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
</configuration>
Interface:
[ServiceContract()]
public interface ILoginService
{
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/username={username}&password={password}")]
[OperationContract]
Message Login(string username, string password);
}
Service implementation:
public class LoginService : ILoginService
{
public Message Login(string username, string password)
{
Message message = new Message();
SQLWorks sqlWorks = new SQLWorks();
string strSession = sqlWorks.Login(username, password);
string strMessage;
message.Session = strSession;
if(strSession == "")
{
strMessage = "Login failed! Please check your username/password!";
}
else
{
strMessage = "Login Successful";
}
message.ErrorMessage = strMessage;
return message;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了连接到您的服务,它需要托管在公共 Web 服务器上。 您使用的地址 192.168.1.5:8000 看起来像是家庭网络地址,无法从外界(电话)访问。
In order to connect to your service, it needs to be hosted on a public web server. It looks like the address you're using 192.168.1.5:8000 is a home network address, which is not accessible from the outside world (a phone).
如果您使用的是 Vista 操作系统,则必须将地址添加到防火墙中
netsh http add urlacl url=http://+:8000 / 用户=域\用户
if you are using vista OS ,you have to add the address into your firewall
netsh http add urlacl url=http://+:8000/ user=DOMAIN\user
使用您的 LAN ip 将模拟器与 Web 服务连接,例如: http://192.168.1.78:8080/webservice
但您的本地连接必须启用
转到:控制面板\网络和 Internet\网络连接
Use your LAN ip to connect your emulator with Web Service,,For EX: http://192.168.1.78:8080/webservice
But your Local Area Connection must be enable
Goto: Control Panel\Network and Internet\Network Connections