如何从Web应用程序调用Windows服务应用程序中托管的WCF服务?

如何从Web应用程序调用Windows服务应用程序中托管的WCF服务?

问题描述:

我想从Web应用程序调用Windows服务应用程序中的方法。为此,我在Windows服务应用程序中托管了WCF服务。但是我不知道如何从Web应用程序中调用Windows服务应用程序中托管的WCF服务中的方法。

我能否知道是否可以从Web应用程序与托管在其中的WCF服务进行通信Windows服务应用程序以你称之为web api的方式? (我尝试使用Angularjs调用WCF服务)

如果是这样,任何人都可以帮我一个例子。

我是学生并原谅我,如果我说的话事实错误。



我尝试过:



我的WCF服务代码



HelloService.cs



I wanted to call a method in a windows service application from a web application. To do so i hosted a WCF service in Windows service application. Yet I have no any idea, how to call a method in WCF service hosted in windows service application from web application.
can i know whether is it possible to communicate from web application to a WCF service hosted in windows service application in a way you call a web api? (I try to call the WCF service using Angularjs)
If so can anyone help me with an example.
I'm a student and forgive me if i'm stating facts wrong.

What I have tried:

My WCF service code

HelloService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace HelloService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloService" in both code and config file together.
    
    public class HelloService : IHelloService
    {      
        
        public string GetMessage()
        {
            return "Hello ";
        }
    }
}





IHelloService.cs





IHelloService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace HelloService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHelloService" in both code and config file together.
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetMessage",
      ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml,
      BodyStyle = WebMessageBodyStyle.Bare)]
        string GetMessage();
    }
}





我的Windows服务应用程序代码



HelloWindowsService.cs





My Windows service application code

HelloWindowsService.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace WindowsServiceHost
{
   
    public partial class HelloWindowsService : ServiceBase
    {
        ServiceHost host;
        public HelloWindowsService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            host = new ServiceHost(typeof(HelloService.HelloService));
            host.Open();
        }

        protected override void OnStop()
        {
            host.Close();
        }
    }
}





App.config





App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehavior0">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="mexBehavior0" name="HelloService.HelloService">
                <clear />
                <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService">
                  </endpoint>
              <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
              </endpoint>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>







web应用程序代码



HelloService.js






web Application code

HelloService.js

app.service('HelloService', ['$http', '$q', function ($http, $q) {

   
    var msg = " ";

    this.hello = function () {

        var error;
        var deferred = $q.defer();

        $http({
            type: "GET",
            url: "http://localhost:8080/HelloService/GetMessage",
        }).then(function successCallback(result) {
            msg = result.data;
            deferred.resolve();
        }, function errorCallback(err) {
            error = err;
            deferred.reject();
        });
        return deferred.promise;
    };

    this.getMsg = function () {
        return msg;
    };


}]);





HelloControl.js





HelloControl.js

app.controller('HelloControl', ['$scope', 'HelloService', function ($scope, HelloService) {

   
    $scope.msg = " ";

    $scope.hello = function () {
        HelloService.hello().then(function (result) {
        $scope.msg = HelloService.getMsg();
        });
    };

}]);





App.js





App.js

var app = angular.module('HelloApp', []);

http ','


q',function(
q', function (


http,