无法通过LAN调用VCF WebService [英] Unable to call a VCF WebService across LAN

查看:73
本文介绍了无法通过LAN调用VCF WebService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 A初学者了解Windows Communication Foundation(WCF)的教程 [ ^ ]但在尝试通过LAN调用WCF服务时出现问题。我在我的iis服务器(192.168.1.106)上托管了WCF服务,并尝试从位于服务器本身或另一台机器上的客户端应用程序调用它。我总是得到以下异常:

I tried the A Beginner's Tutorial for Understanding Windows Communication Foundation (WCF)[^] but got problem when trying to call the WCF service across my LAN. I hosted the WCF service on my iis server (at 192.168.1.106) and tried to call it from the client app located on the server itself or on another machine. I always get the following exception:

Unhandled Exception: System.ServiceModel.EndpointNotFoundException: There was no
 endpoint listening at http://192.168.1.106/WebSite/WcfServiceLibrary1/PairArithmeticService 
 that could accept the message. This is often caused by an incorrect address or SOAP.



这是WCFServiceLibrary1的App.config:


Here is the App.config of the WCFServiceLibrary1:

  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.PairArithmeticService">
        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IPairArithmeticService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.1.106/WebSite/WcfServiceLibrary1/PairArithmeticService/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>



我在服务器上成功发布了它并在c:\inetpub\wwwroot文件夹中获得了以下Web.config文件:


I published it succesfully on the server and got the following Web.config file in the c:\inetpub\wwwroot folder:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.PairArithmeticService">
        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IPairArithmeticService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.1.106/WebSite/WcfServiceLibrary1/PairArithmeticService/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>





在客户端,我添加了服务参考并获得了以下App.config文件:



On the Client side, I added a Service Reference and got the following App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IPairArithmeticService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://192.168.1.106/WebSite/WcfServiceLibrary1/PairArithmeticService/"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPairArithmeticService"
                contract="ServiceReference1.IPairArithmeticService" name="BasicHttpBinding_IPairArithmeticService" />
        </client>
    </system.serviceModel>
</configuration>





这是我的WCF客户表格代码:



Here is my WCF Client Form code:

namespace TestWcfServiceLibrary1WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            PairArithmeticServiceClient client = new PairArithmeticServiceClient();
            Pair pair1 = new Pair();
            Pair pair2 = new Pair();
            Pair resultPair;

            try
            {
                pair1.First = int.Parse(tbxPair1First.Text);
            }
            catch (FormatException)
            {
                tbxPair1First.Text = "0";
                pair1.First = 0;
            }
            try
            {
                pair1.Second = int.Parse(tbxPair1Second.Text);
            }
            catch (FormatException)
            {
                tbxPair1Second.Text = "0";
                pair1.Second = 0;
            }
            try
            {
                pair2.First = int.Parse(tbxPair2First.Text);
            }
            catch (FormatException)
            {
                tbxPair2First.Text = "0";
                pair2.First = 0;
            }
            try
            {
                pair2.Second = int.Parse(tbxPair2Second.Text);
            }
            catch (FormatException)
            {
                tbxPair2Second.Text = "0";
                pair2.Second = 0;
            }

            resultPair = client.Add(pair1, pair2);

            tbxResultFirst.Text = resultPair.First.ToString();
            tbxResultSecond.Text = resultPair.Second.ToString();
        }

        private void btnSubstract_Click(object sender, EventArgs e)
        {
            PairArithmeticServiceClient client = new PairArithmeticServiceClient();
            Pair pair1 = new Pair();
            Pair pair2 = new Pair();
            Pair resultPair;

            try
            {
                pair1.First = int.Parse(tbxPair1First.Text);
            }
            catch (FormatException)
            {
                tbxPair1First.Text = "0";
                pair1.First = 0;
            }
            try
            {
                pair1.Second = int.Parse(tbxPair1Second.Text);
            }
            catch (FormatException)
            {
                tbxPair1Second.Text = "0";
                pair1.Second = 0;
            }
            try
            {
                pair2.First = int.Parse(tbxPair2First.Text);
            }
            catch (FormatException)
            {
                tbxPair2First.Text = "0";
                pair2.First = 0;
            }
            try
            {
                pair2.Second = int.Parse(tbxPair2Second.Text);
            }
            catch (FormatException)
            {
                tbxPair2Second.Text = "0";
                pair2.Second = 0;
            }

            resultPair = client.Subtract(pair1, pair2);

            tbxResultFirst.Text = resultPair.First.ToString();
            tbxResultSecond.Text = resultPair.Second.ToString();
        }
    }
}





好​​奇的是我总是得到EndpointNotFoundException从任何局域网机器甚至从服务器本身运行客户端,但是当我从VisualStudio(Debug / StartNewInstance)运行它并且如果以管理员(在服务器机器上)启动VisualStudio时,我没有例外一个WCF调用正常工作!但是如果正常启动VisualStudio我会得到EndpointNotFoundException ...



我也尝试以管理员身份启动我的客户端应用程序(从VS出来)但它也失败了EndpointNotFoundException(来自服务器机器或来自其他机器)...



感谢您的帮助。



The curious thing is that I always get the EndpointNotFoundException when I run the Client from any LAN machine or even from the server itself, but when I run it from VisualStudio (Debug/StartNewInstance) and if VisualStudio was launched as Administrator (on the server machine) I get no exception an the WCF call works properly !!! But if VisualStudio was launched normally I get the EndpointNotFoundException...

I also tried to launch my Client App as Administrator (Out of VS) but it failed too with the EndpointNotFoundException (either from the server machine or from another machine)...

Thank you for any help.

推荐答案

我终于通过以下更正解决了通信问题:



1)在客户端,我的App.config文件引用了错误的端点地址。以下是正确的App.config内容:



I finally solved the communication issue by the following corrections:

1) On the Client side, my App.config file refered a wrong endpoint address. Here is the correct App.config content:

<configuration>
    <startup> 
        <supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.servicemodel>
        <bindings>
            <basichttpbinding>
                <binding name="BasicHttpBinding_IPairArithmeticService" />
            </basichttpbinding>
        </bindings>
        <client>
            <endpoint address="http://192.168.1.106/WebSite/WcfServiceLibrary1.PairArithmeticService.svc">
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPairArithmeticService"
                contract="ServiceReference1.IPairArithmeticService" name="BasicHttpBinding_IPairArithmeticService" />
        </endpoint></client>
    </system.servicemodel>
</configuration>





2)在服务器端,我在http:// localhost上发布了WCFServiceLibrary1,I必须在refered / WebSite文件夹中发布它:http:// localhost / WebSite



3)经过这些修复,我没有得到EndpointNotFoundException,但是我得到一个ProtocolException Unhandled,(405)当客户端试图调用WCF服务方法时不允许使用方法。

我终于发现这是由于.Net 4.5平台中的配置问题:HTTP激活功能未启用!

现在WCF调用最终正常工作。



2) On the server side I published the WCFServiceLibrary1 on http://localhost, I had to publish it in the refered /WebSite folder: http://localhost/WebSite

3) After these fixes, I didn't got the EndpointNotFoundException anymore, but I got a ProtocolException Unhandled, (405) Method not allowed when the Client tried to invoke a WCF service method.
I finally found this was due to a configuration issue in my .Net 4.5 platform: The HTTP Activation feature was not enabled !
Now the WCF calls finally work properly.


这篇关于无法通过LAN调用VCF WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆