在公共 IP 上托管 WCF 服务

本文关键字:WCF 服务 IP | 更新日期: 2023-09-27 18:37:02

我在本地创建了一个 WCF 服务,并在 IIS 上承载该服务,以便我可以从其他 PC 调用我的 WCF 服务。两台电脑位于同一路由器下。我可以在本地调用 WCF 服务,但无法从其他 PC 浏览相同的服务。

网络.config

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    'Windows'Microsoft.Net'Framework'v2.x'Config 
-->
<configuration>
  <appSettings />
  <connectionStrings />
  <system.web>
    <!--
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
    <compilation debug="true" targetFramework="4.0" />
    <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
    <authentication mode="Windows" />
    <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
  </system.web>
  <system.web.extensions>
    <scripting>
      <webServices>
        <!--
              Uncomment this section to enable the authentication service. Include 
              requireSSL="true" if appropriate.
          <authenticationService enabled="true" requireSSL = "true|false"/>
          -->
        <!--
              Uncomment these lines to enable the profile service, and to choose the 
              profile properties that can be retrieved and modified in ASP.NET AJAX 
              applications.
          <profileService enabled="true"
                          readAccessProperties="propertyname1,propertyname2"
                          writeAccessProperties="propertyname1,propertyname2" />
          -->
        <!--
              Uncomment this section to enable the role service.
          <roleService enabled="true"/>
          -->
      </webServices>
      <!--
        <scriptResourceHandler enableCompression="true" enableCaching="true" />
        -->
    </scripting>
  </system.web.extensions>
  <!--
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
  <system.serviceModel>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>        
      <service name="WcfStudentService.StudentService" behaviorConfiguration="WcfStudentService.StudentServiceBehavior">
        <!-- Service Endpoints -->
        <endpoint address=""  binding="wsHttpBinding" contract="WcfStudentService.IStudentService">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost" />
              <!--<dns value="123.236.41.136" />-->
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfStudentService.StudentServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>      
  </system.serviceModel>
    <system.webServer>
        <defaultDocument>
            <files>
                <add value="StudentService.svc" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>

学生服务.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfStudentService
{
    // StudentService is the concrete implmentation of IStudentService.
    public class StudentService : IStudentService
    {
        List<StudentInformation > Students = new List<StudentInformation>() ;
        // Create  list of students
        public StudentService()
        {
            Students.Add(new StudentInformation(1001, "Nikhil", "Vinod"));
            Students.Add(new StudentInformation(1002, "Joshua", "Hunter"));
            Students.Add(new StudentInformation(1003, "David", "Sam"));
            Students.Add(new StudentInformation(1004, "Adarsh", "Manoj"));
            Students.Add(new StudentInformation(1005, "HariKrishnan", "Vinayan"));
        }
        // Method returning the Full name of the student for the studentId
        public string GetStudentFullName(int studentId)
        {
            IEnumerable<string> Student
                         = from student in Students
                           where student.StudentId == studentId
                           select student.FirstName + " " + student.LastName;
            return Student.Count() != 0 ? Student.First() : string.Empty; 
        }
        // Method returning the details of the student for the studentId
        public IEnumerable<StudentInformation> GetStudentInfo(int studentId)
        {
            IEnumerable<StudentInformation> Student =  from student in Students
                                                       where student.StudentId == studentId
                                                       select student ;
            return Student;
        }

    }
}

是服务.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfStudentService
{
    // Defines IStudentService here
    [ServiceContract ]
    public interface IStudentService
    {
        // Define the GetStudentFullName OperationContact here….
        [OperationContract]
        String GetStudentFullName(int studentId);
        // Define the GetStudentInfo OperationContact here….
        [OperationContract]
        IEnumerable<StudentInformation> GetStudentInfo(int studentId);
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class StudentInformation
    {
        int _studentId ;
        string _lastName;
        string _firstName;
        public StudentInformation(int studId, string firstname, string lastName)
        {
            _studentId = studId;
            _lastName = lastName;
            _firstName = firstname;
        }
        [DataMember]
        public int StudentId
        {
            get { return _studentId; }
            set { _studentId = value; }
        }
        [DataMember]
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }
        [DataMember]
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }
    }
}

我用谷歌搜索这个问题,发现了许多类似的问题,但无法理解我的解决方案。我发现一个线程几乎没有帮助,但仍然无法得到它。

我的服务主机在 192.168.X.XXX:82,当我在其他PC上浏览相同的主机时,它显示"网络问题"。请帮助我摆脱困境。

在公共 IP 上托管 WCF 服务

我在系统上添加了入站和出站规则,并允许端口 82 用于"域、私有和公共"。您可以设置规则"控制面板-->所有控制面板项->Windows防火墙-->高级设置"。

希望这对某人有帮助。