简体   繁体   中英

Host WCF Service on Public Ip

I have created a WCF service on my local and host the service on IIS so that I can invoke my WCF service from other PC. Both PCs are under the same router. I am able to call WCF service on my local but not able to browse the same service from other PC.

Web.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>

StudentService.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;
        }


    }
}

IStudentService.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; }
        }
    }
}

I google this issue and found many similar issues but not able to understand the resolution of mine. One thread I found little helpful but still not able to get it.

My service host on 192.168.X.XXX:82 , when I browse the same on other PC it shows "Network Issue". Please help me to get out from this.

I have added an Inbound and Outbound Rules on my system and allow Port 82 for "Domain, Private and Public". You can set the Rules " Control Panel-->All Control Panel Items->Windows Firewall-->Advance Settings ".

Hope this helps to someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM