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
{
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
{
[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', []);