C# Web服务不与应用程序交互
问题: (最后的解决方案) 我在一个网络项目中得到了一个Silverlight应用程序 网 银光 web包含一项服务:C# Web服务不与应用程序交互,c#,.net,silverlight,web-services,asmx,C#,.net,Silverlight,Web Services,Asmx,问题: (最后的解决方案) 我在一个网络项目中得到了一个Silverlight应用程序 网 银光 web包含一项服务: [WebService(Namespace = "svChat")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncommen
[WebService(Namespace = "svChat")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
//[System.Web.Script.Services.ScriptService]
public class GetIPService : System.Web.Services.WebService
{
public GetIPService ()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string GetIp()
{
return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
}
我在Silverlight应用程序中使用该服务获得了一个类:
public class Client
{
private string ip;
private string created;
#region Properties
public string Ip
{
get { return ip; }
set { ip = value; }
}
public string Created
{
get { return created; }
set { created = value; }
}
#endregion
public Client()
{
}
public void SetIp()
{
ServiceReference1.GetIPServiceSoapClient scIpClient = new svChat.ServiceReference1.GetIPServiceSoapClient();
scIpClient.GetIpCompleted += new EventHandler<svChat.ServiceReference1.GetIpCompletedEventArgs>(IpService_Completed);
scIpClient.GetIpAsync();
}
private void IpService_Completed(object sender, ServiceReference1.GetIpCompletedEventArgs e)
{
this.ip = e.Result;
}
}
公共类客户端
{
私有字符串ip;
创建私有字符串;
#区域属性
公共字符串Ip
{
获取{返回ip;}
设置{ip=value;}
}
已创建公共字符串
{
获取{return created;}
集合{created=value;}
}
#端区
公共客户机()
{
}
公共无效SetIp()
{
ServiceReference1.GetIPServiceSoapClient scIpClient=new svChat.ServiceReference1.GetIPServiceSoapClient();
scIpClient.GetIpCompleted+=新事件处理程序(IpService\u已完成);
scIpClient.GetIpAsync();
}
私有void IpService_已完成(对象发送方,servicerence1.GetIpCompletedEventArgs e)
{
此ip=e.结果;
}
}
创建客户机后,将调用SetIp(),并将Client.Ip添加到文本框中。
什么也没发生。
Ip=null
服务本身工作正常,经过测试。
通过上述代码获得Ip是可行的
通过Silverlight应用程序通过服务获取Ip不起作用
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="GetIPServiceSoap" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2090/svChat.Web/GetIPService.asmx"
binding="basicHttpBinding" bindingConfiguration="GetIPServiceSoap"
contract="ServiceReference1.GetIPServiceSoap" name="GetIPServiceSoap" />
</client>
</system.serviceModel>
</configuration>
有什么想法吗
问候,
解决方案:
在VS 2010(旗舰版)中创建Silverlight应用程序会导致VS对Silverlight应用程序和网站使用相同的测试服务器。
在VS使用Silverlight配置来设置测试服务器之前,这是没有问题的。
silverlight客户端现在将无法正确访问Web服务器的Web服务。
确切原因尚不清楚,但我认为这是由上述情况造成的。
所以,开始调试,等待网站加载并弹出“异常”,然后“停止”调试,继续测试网站而不必担心异常。
缺点:没有调试。我想关键是要确认对Web服务的请求确实包含Http头
Http\u X\u FORWARDED\u FOR
,这通常是由代理服务器或负载平衡器添加的
如果此标头不存在,则调用的结果
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
为空,这就是您所看到的。由于您在配置中显示的端点指向本地主机,因此您肯定没有通过代理或负载平衡器,因此不会为头添加HTTP\u X\u FORWARDED\u
http://localhost:2090/svChat.Web/GetIPService.asmx
如果您没有使用代理或负载平衡器,可以使用REMOTE_ADDR(不同程度的成功)
无论如何,您可能应该编写代码来处理这样一个事实,即这两个函数实际上都没有任何设置。除非控制客户端和服务器之间的所有基础设施组件,否则不能假设每个代理或lad平衡器都会为
头添加HTTP\u X\u FORWARDED\u
更新:根据您提供的代码,以下是我所做的更改。
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
this.MainClient = new Client();
ClientList.Clients.Add(this.MainClient);
// Removed LoadXMLFile call here, constructor runs before Loaded event.
//LoadXMLFile();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
svChat.ServiceReference1.GetIPServiceSoapClient scIpClient = new svChat.ServiceReference1.GetIPServiceSoapClient();
scIpClient.GetIpCompleted += new EventHandler<svChat.ServiceReference1.GetIpCompletedEventArgs>(IpService_Completed);
scIpClient.GetIpAsync();
}
public void IpService_Completed(object sender, svChat.ServiceReference1.GetIpCompletedEventArgs e)
{
this.MainClient.Ip = e.Result;
// Probably where you should call LoadXMLFile
// at this point the async call has returned and
// the ip is intitialized.
LoadXMLFile();
}
public主页()
{
初始化组件();
this.Loaded+=新的RoutedEventHandler(已加载页面);
this.MainClient=新客户端();
ClientList.Clients.Add(this.MainClient);
//已删除此处的LoadXMLFile调用,构造函数在加载事件之前运行。
//LoadXMLFile();
}
已加载私有无效页面(对象发送方,路由目标e)
{
svChat.ServiceReference1.GetIPServiceSoapClient scIpClient=新的svChat.ServiceReference1.GetIPServiceSoapClient();
scIpClient.GetIpCompleted+=新事件处理程序(IpService\u已完成);
scIpClient.GetIpAsync();
}
public void IpService_已完成(对象发送方,svChat.servicerence1.GetIpCompletedEventArgs e)
{
this.MainClient.Ip=e.结果;
//可能是您应该调用LoadXMLFile的地方
//此时,异步调用已返回并
//ip已初始化。
LoadXMLFile();
}
@daemonfire300,我在那里做了一个快速测试应用程序,效果很好。您可能会考虑使用像FIDDLE这样的工具来查看HTTP流量。daemonfire300,是的,我使用了ASMX网络服务。您可以在此处下载测试代码,请注意,您需要更新引用,因为我使用了内部VS web服务器,所以端口号将在您的计算机上更改。@daemonfire300,如果您将代码放在某个地方,我将很乐意查看。@daemonfire300,好的,我快速查看了一下。我发现了我认为的问题所在。在MainPage的构造函数中调用LoadXMLFile(),在该构造函数中尝试访问此.MainClient.Ip.ToString(),但此.MainClient.Ip仍然为空,因为加载的页面尚未运行,该页面调用scIpClient.GetIpAsync。您似乎有一个不同的服务引用到了项目中的服务,GetIpAsync需要一些我没有的请求对象,所以我删除了服务引用并重新添加了它,一切都很好。@daemonfire300,好的,上传到我的skydrive这是我的荣幸,希望你能做到这一点。为什么不在客户端和服务器上都使用WCF呢?
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
this.MainClient = new Client();
ClientList.Clients.Add(this.MainClient);
// Removed LoadXMLFile call here, constructor runs before Loaded event.
//LoadXMLFile();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
svChat.ServiceReference1.GetIPServiceSoapClient scIpClient = new svChat.ServiceReference1.GetIPServiceSoapClient();
scIpClient.GetIpCompleted += new EventHandler<svChat.ServiceReference1.GetIpCompletedEventArgs>(IpService_Completed);
scIpClient.GetIpAsync();
}
public void IpService_Completed(object sender, svChat.ServiceReference1.GetIpCompletedEventArgs e)
{
this.MainClient.Ip = e.Result;
// Probably where you should call LoadXMLFile
// at this point the async call has returned and
// the ip is intitialized.
LoadXMLFile();
}