Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
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

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();
    }
}


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

<?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 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

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

   
    $scope.msg = " ";

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

}]);


App.js

var app = angular.module('HelloApp', []);
Posted
Updated 29-Jan-17 20:44pm
Comments
Afzaal Ahmad Zeeshan 27-Jan-17 5:11am    
Can your web application access the address where your WCF is hosted? It works in most local cases, but for internet-based applications, you need to ensure a lot of stuff.
Member 12963196 27-Jan-17 5:21am    
@Afzaal, Thanks for the comment. I'm new to these things and working on to find a way to communicate from browser to a windows service application. Then i found about wcf service which can be hosted in windows service application. do you think it is imposible? :(
Afzaal Ahmad Zeeshan 27-Jan-17 8:22am    
No, it is not, but for that you need to make sure that the endpoints (the URL) where you are accessing the service, it actually the real endpoint, where the WCF is being hosted at.
Member 12963196 29-Jan-17 22:57pm    
@Afzaal i can try the endpoint url just typing on browser. But unable to call it through web application :(
Member 12963196 29-Jan-17 23:27pm    
@Afzaal thannk you for helping me. Finally able to pass a string data from web application to Windows Service application :)

Hi,

Consume WCF from javascript : Dotnet by Example: Calling a WCF service from Javascript[^]
 
Share this answer
 
Comments
Member 12963196 27-Jan-17 5:16am    
@Suvabrata, Thanks for your help. But what i want is to call a wcf service WHICH IS HOSTED IN A WINDOWS SERVICE APPLICATION from WEB APPLICATION. :(
Suvabrata Roy 27-Jan-17 5:40am    
Hello, for a self-hosted service, you must enable JSONP for AJAX clients to consume it. To enable, JSONP, set the CrossDomainScriptAccessEnabled property on the WebHttpBinding to true. As for how to consume the service,

Ref : https://social.msdn.microsoft.com/Forums/vstudio/en-US/2c3dc15a-b019-480c-82a5-b85765dcd761/consuming-a-selfhosted-wcf-web-service-with-custom-arguments-in-javascript?forum=wcf
Member 12963196 27-Jan-17 5:59am    
@Suvabrata, thank you so much. I modify my app config with CrossDomainScriptAccessEnabled property to true. but still having some problems with calling it from Web Application. :(
Suvabrata Roy 27-Jan-17 6:01am    
Will you please post the actual error you are having...
Member 12963196 27-Jan-17 6:01am    
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.servicemodel>
<behaviors>
<endpointbehaviors>
<behavior name="webBehavior">
<webHttp helpEnabled="true" defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json"
automaticFormatSelectionEnabled="false" />


<servicebehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true" />



<bindings>
<webHttpBinding >
<binding name="webBindingConfiguration" crossDomainScriptAccessEnabled="true" />


<serviceHostingEnvironment multipleSiteBindingsEnabled="true"
aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="mexBehavior" name="HelloServiceLibrary.HelloService">
<endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding"
bindingConfiguration="webBindingConfiguration" contract="HelloServiceLibrary.IHelloService" />
<host>
<baseaddresses>
<add baseAddress="http://localhost:8070/" />





Hi everyone!!!
First of all thank you so much for helping me to achieve my goal. My goal was to call a method in a windows service application from a web application.
To achieve that i tried with WCF service.
problem i faced through was
no-access-control-allow-origin-header-is-present-on-the-requested-resource-err
.
so i was unable to find a better solution for that.
but with the help of @Jon McKee with Web API Self-Hosting Using Windows Service: Part 1[this] article i was able to achieve my goal without
WCF service.
here also i got
no-access-control-allow-origin-header-is-present-on-the-requested-resource-err
.
i corrected it with the help of
System.Web.Http.Cors
.
Thank you everyone!
 
Share this answer
 
Comments
Jon McKee 30-Jan-17 3:19am    
Ah, CORS. This is why JSONP was created =)
Member 12963196 30-Jan-17 3:58am    
@Jon I'm not familiar with JSONP. :( Cant I use JSONP with angularjs?
Jon McKee 30-Jan-17 4:03am    
JSONP is basically just JSON that is wrapped in a function call and embedded as a script to bypass CORS. You can use JSONP if the service supports returning data as JSONP instead of plain JSON. An excellent summation can be found here. Or here if you want a more technical explanation.
Member 12963196 30-Jan-17 4:14am    
@Jon Thank you so much. I will try that stuff too. :)
Jon McKee 30-Jan-17 4:16am    
I added to my response if you wanted a more technical and less "this is how you use it" explanation =) I'm glad you're so interested in different technologies! The world needs more of that =D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900