WCF Homestay (host) self-hosted (self-hosting) Simple instance "Windows Application Host"

Source: Internet
Author: User
Tags hosting

Tag: ODI for Style project CLI cannot connect using proc task

Objective:

  The host principle method of self-hosted by various applications is similar, therefore: here directly on the case!

Step one: Create service contracts and services

1. Create a new solution: Add a WCF Service library project.

2, in order to demonstrate, I have automatically generated the interface and the implementation of the interface class Delete, add a WCF Service myself

3, write the service function (at the same time, because the original auto-generated interface and class deleted, it is necessary to make the configuration file corresponding changes:)

Namespace Wcfself02
{
Note:you can use the ' Rename ' command on the ' Refactor ' menu to change the interface name ' ISERVICE02 ' in both code an d config file together.
[ServiceContract]
public interface IMe02
{
[OperationContract]
String ShowName (String str);
}
}
Namespace Wcfself02
{
Note:you can use the ' Rename ' command on the ' Refactor ' menu to change the class name ' Service02 ' in both Code and con Fig file together.
public class Me02:ime02
{

String Ime02.showname (String str)
{
String STRR;
STRR = str;
Console.WriteLine (STRR);
Return "La la la" + strr;
}
}
}

Step Two: Create a service Host

Create a Windows application to implement a self-hosted WCF service "Add a Windows application, introduce a wcfservice.library_01 reference, add a using System.ServiceModel; a library file reference. ", the specific implementation as well as the code is as follows:

Program.cs:

Using System;
Using System.Collections.Generic;
Using System.Diagnostics;
Using System.Linq;
Using System.Security.Principal;
Using System.Threading.Tasks;
Using System.Windows.Forms;

Namespace wcfhost02
{
Static Class Program
{
<summary>
The main entry point for the application.
</summary>

[STAThread]
static void Main ()
{
Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (FALSE);

Switch (checkwindowsidentity ())
{
Case 0:application.run (New Form1 ()); Break
Case 1:application.exit (); Break
}

}

static int checkwindowsidentity ()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent ();
WindowsPrincipal principal = new WindowsPrincipal (identity);
if (principal. IsInRole (windowsbuiltinrole.administrator)) return 0; Administrator

Normal user, start the program with the startup object to ensure that the startup object is created with administrator run
ProcessStartInfo startinfo = new ProcessStartInfo ();
Startinfo.useshellexecute = true;
Startinfo.workingdirectory = environment.currentdirectory;
Startinfo.filename = Application.executablepath;
Startinfo.verb = "RunAs";
Try
{
Process.Start (StartInfo);
return 1;//Normal User
}
Catch
{
return-1;//not authorized to run or user abandoned
}

}

}
}

Form1.cs:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Net;
Using System.ServiceModel;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using Wcfself02;
Namespace wcfhost02
{
public partial class Form1:form
{
Me02 MM = new Me02 ();
ServiceHost host = null;
Public Form1 ()
{
InitializeComponent ();
}

private void Open_click (object sender, EventArgs e)
{
if (host==null)
{
Host = new ServiceHost (typeof (ME02));
Host. Open ();
}
}

private void Close_click (object sender, EventArgs e)
{
if (host!=null)
{
Host. Close ();
host = null;
}
}

private void Form1_Load (object sender, EventArgs e)
{
Close_click (sender, E);
}
}
}

Use the form of a configuration file to define endpoint additions and service behavior

App.config[one configuration method]:

<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name= "Metadatabehavior" >
<servicemetadata httpgetenabled= "true" Httpgeturl= "http://localhost:8733/Design_Time_Addresses/wcfself02/Me02 /metadata "/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorconfiguration= "Metadatabehavior" name = "wcfself02. Me02 ">
<endpoint address= "http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/"
Binding= "Wshttpbinding"
Contract= "WCFSELF02. IMe02 "/>
</service>
</services>
</system.serviceModel>
</configuration>

Compile the host program, open the banner. exe file as an administrator at the location of the file, and click "Open" to enter the address in the configuration file (http://localhost:8733/Design_Time_Addresses/wcfself02/Me02 /metadata), open, click "Close", unable to connect, instructions to create success!!

App.config[one of the first configuration methods]:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>

<appSettings>
<add key= "Aspnet:usetaskfriendlysynchronizationcontext" value= "true"/>
</appSettings>
<system.web>
<compilation debug= "true"/>
</system.web>
<!--when deploying the Service library project, the content of the config file must is added to the host ' s
App. Config file. System.Configuration does not the support config files for libraries. -
<system.serviceModel>
<services>
<service name= "wcfself02. Me02 ">
<endpoint address= "" binding= "BasicHttpBinding" contract= "wcfself02. IMe02 ">
<identity>
<dns value= "localhost"/>
</identity>
</endpoint>
<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>
<baseAddresses>
<add baseaddress= "http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/"/>
</baseAddresses>
</service>
<service name= "wcfself02. Me02 ">
<endpoint address= "" binding= "BasicHttpBinding" contract= "wcfself02. IMe02 ">
<identity>
<dns value= "localhost"/>
</identity>
</endpoint>
<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>
<baseAddresses>
<add baseaddress= "http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/"/>
</baseAddresses>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!--to avoid disclosing metadata information,
Set the values below to false before deployment--
<servicemetadata httpgetenabled= "true" httpsgetenabled= "true"/>
<!--to receive exception details on faults for debugging purposes,
Set the value below to true. Set to False before deployment
To avoid disclosing exception information--
<servicedebug includeexceptiondetailinfaults= "False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

</configuration>

Compile the host program, open the banner. exe file as an administrator at the location of the file, and click "Open" to enter the address in the configuration file (http://localhost:8733/Design_Time_Addresses/wcfself02/Me02 /), open, click "Close", unable to connect, instructions to create success!!

Special attention!! When running the host application, be sure to run the host application with Administrator privileges!!!

Step three: Create a client (reference service, verify the services created above)

Create a console application that references the above services as a client, and when adding service apps, be aware that the service is turned on!!!

Reference the function of the service side, can realize the corresponding function, here not more than repeat!

"Welcome to Reprint"

Reprint please indicate source: le study

WCF Homestay (host) self-hosted (self-hosting) Simple instance "Windows Application Host"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.