Due to work needs, in recent days in the study of Windows 2008 how to host WCF MSMQ in IIS, the middle encountered a lot of problems, now the operation method to tidy up, convenient for other friends reference.
First, write the service-side code
In this example, add the WCF service Mygreeting.svc, the server-side code is as follows:
1. Service Contract
1 usingSystem.ServiceModel;2 3 namespaceIismsmqserver4 {5 [ServiceContract]6 Public Interfaceimygreeting7 {8[OperationContract (IsOneWay =true)]9 voidHello (stringname);Ten } One}
View Code2. Service Realization
1 usingSystem;2 usingSystem.IO;3 usingSystem.Web.Hosting;4 5 namespaceIismsmqserver6 {7 Public classmygreeting:imygreeting8 {9 Public voidHello (stringname)Ten { One stringmsg =string. Format ("{0}[info] ... Hello, {1}", DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss"), name); A stringFileName = Path.Combine (Hostingenvironment.mappath ("~"),"Log","my log. txt"); - texthelper.writelineappend (FileName, msg); - } the } -}
View CodeSecond, configuration services
Use the Edit WCF Configuration tool or manually edit the WCF config file, which is bound to netmsmqbinding:
<?XML version= "1.0"?><Configuration> <appSettings> <AddKey= "Aspnet:usetaskfriendlysynchronizationcontext"value= "true"/> </appSettings> <system.web> <compilationtargetframework= "4.5"Debug= "true"/> <HttpRuntimetargetframework= "4.5"/> </system.web> <System.ServiceModel> <Behaviors> <servicebehaviors> <Behaviorname= "Msmqbehav"> <Servicemetadatahttpgetenabled= "true"/> </Behavior> </servicebehaviors> </Behaviors> <Bindings> <netMsmqBinding> <bindingname= "MSMQ"Durable= "false"useactivedirectory= "false"Deadletterqueue= "Custom"exactlyonce= "false"> <SecurityMode= "None"> <TransportMsmqAuthenticationMode= "None"Msmqprotectionlevel= "None"/> <messageclientCredentialType= "None"/> </Security> </binding> </netMsmqBinding> </Bindings> <Services> <Servicebehaviorconfiguration= "Msmqbehav"name= "Iismsmqserver.mygreeting"> <EndpointAddress= "Net.msmq://localhost/private/huatao"binding= "NetMsmqBinding"bindingconfiguration= "MSMQ"Contract= "Iismsmqserver.imygreeting"/> </Service> </Services> </System.ServiceModel> <system.webserver> <Modulesrunallmanagedmodulesforallrequests= "true"/> <!--to browse Web apps root directory during debugging, set the value below to true. Set to false before deployment to avoid disclosing web App folder information. - <Directorybrowseenabled= "false"/> </system.webserver></Configuration>
View CodeIii. creating a Message Queue 1, creating a queue
The service address that you just configured in the configuration file is: Net.msmq://localhost/private/huatao, you need to create a private Message Queuing "Huatao" in the server.
2. Configure Message Queuing permissions
Select the Message Queuing Huatao that you just created, right-click Properties, and in the Security tab, configure Full Control permissions for the Everyone user and the ANONYMOUS LOGON user.
3. Modify the Registration Form
If you want programs on other servers to send messages to this message queue, you also need to modify the registry as follows:
- Registry New HKLM\SOFTWARE\MICROSOFT\MSMQ\PARAMETERS\SECURITY\ALLOWNONAUTHENTICATEDRPC entry, set its DWORD value to 1
- Registry new Hklm\software\microsoft\msmq\parameters\security\newremotereadserverdenyworkgroupclient entry, set its DWORD value to 1
Iv. Hosting WCF Service 1, Publishing Service files in IIS
Select the service item and right-click on "Publish" to publish it to the appropriate file directory in the Explorer.
2. New website
In IIS, create a new Web site in MSMQ, converting the following file directory to application Iismsmqserver.
3. Add NET.MSMQ Protocol
Because IIS is a service that cannot host non-HTTP protocols directly, the Web site cannot be browsed directly, and the NET.MSMQ protocol needs to be added to the site and the application.
(1) Add NET.MSMQ protocol bindings for Web sites
Select Site MSMQ and right-click Edit bindings.
Click the "Add" button in the pop-up website bindings window to add the NET.MSMQ protocol bindings.
(2) Add NET.MSMQ protocol support for the application
With the application iismsmqserver selected, select the Advanced Settings item.
Add the NET.MSMQ protocol support in the Enable protocol, separated by commas.
(3) Browse Service
Browse service file MYGREETING.SVC.
V. Writing client code
Reference the service in the client.
Call the service again.
1 using System;2 using WCFCLIENT.MSMQ;3 4 namespace WCFClient5 {6 Class Program7 {8 static void Main (string[] args)9 {Ten console.title = "WCF client"; One MSMQ. Mygreetingclient client = new Mygreetingclient (); A client. Hello ("test"); - - console.readline (); the } - } -}
View Code
To view the server, a log file is generated: (Note: The server must create a log directory beforehand)
Vi. reading messages from other machine message queues
If you want to read Message Queuing from another machine, you need to do two things:
1. Modify the service configuration file
Modify the binding address in the WCF service configuration file, such as: Net.msmq://192.168.0.4/private/huatao
2. Re-edit Site protocol bindings
Re-edit the Web site's NET.MSMQ protocol bindings to modify the binding information to the machine IP of the message to be read.
Vii. Summary
This article details how to host the WCF MSMQ service in a Windows 2008 environment in a method where the key points are:
- NET.MSMQ protocol bindings are correct, especially don't forget that the application is also adding NET.MSMQ protocol support.
- Be aware that sufficient permissions are configured for Message Queuing.
Methods for hosting WCF MSMQ in IIS under Windows 2008