共用方式為


Insert MSMQ in WCF Service!

Hello You!

Want to try your hands on with WCF and MSMQ? Here is a fun, easy tutorial for you!

Prerequisites :

1. Windows 8.1

2. Visual Studio 2013 Ultimate

STEP 1: Enable MSMQ in your PC. There are two ways you can do it. First, on your search charm type = "turn windows features on or off"  or Go to Control Panel -> Programs and features -> on your left turn windows features on and off. There put a check mark across all the MSMQ related features!

STEP 3 : Open a WCF project on your Visual Studio 2013. I have named this project as SampleMSMQ.

STEP 2 : Make the queues manually. Our App simple inserts and receives messages from the queues for that the queues must be created in our system. Here is how : Goto Control panel ->Administrative tools -> Computer Management. At the bottom of the left side you can see "Services and Applications". Under there click on Message Queuing. There, right click on Public Queue -> New ->Public Queue. Name is InQueue. MSMQ is not case sensitive. Once this is done, refresh public queue folder , refresh Message queuing folder. After few minutes your queue will be listed under public queue. One more to do is permit everyone to insert and retrieve from the queue. For this, right click on the queue ->InQueue -> properties -> Security -> Here defines the authorization/permission. This step is very important. For myself, I have allowed all permission for first 6 usernames. you can choose who you want to permit and how much you want to permit.

STEP 3: Now, we will start writing methods to send some data to the queue and receive data from the queue. For this, I am defining two interfaces in my IService1.cs file.

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

namespace SampleMSMQ

{

[ServiceContract]

public interface IService1

{

[OperationContract]

void SendToQueue();

[OperationContract]

int ReadFromQueue();

}

}

STEP 4: Next define these methods in our Service1.svc file. Before writing the methods, one important step is to add reference to System.Messaging. For this, right click on SampleMSMQ ->Add -> Add references. Find System.Messaging and select it.  Now write the suing statement for System.Messaging. Write definition of all the classes we have defined in the IService1.cs file now. Here is the code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

using System.Messaging;

namespace SampleMSMQ

{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.

// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.

public class Service1 : IService1

{

public void SendToQueue()

{

try

{

int a = 50;

MessageQueue newObj = CreateQueue(".\\inQueue");

newObj.Send(a);

}

catch (Exception e)

{

Console.WriteLine(e.Message);

}

}

MessageQueue CreateQueue(string queuePath)

{

if (!MessageQueue.Exists(queuePath))

{

return (MessageQueue.Create(queuePath));

}

else

{

return new MessageQueue(queuePath);

}

}

public int ReadFromQueue()

{

Message msg;

int b =0;

try

{

MessageQueue queueObj = new MessageQueue(".\\inQueue");

queueObj.Formatter = new XmlMessageFormatter(new Type[] { typeof(int) });

msg = queueObj.Receive();

b = (int)msg.Body;

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

return b;

}

}

}

STEP 5: Press F5 and run your service! Two things can happen, either your service will open up in the browser, if this happens, click on Servoce1.svc file on the browser and open it in the new tab, OR, service will run like this (below picture). if it is running like the below picture, right click on the link provided and open the link in the browser.

STEP 6: Make the client to run the Service and see the output. We will make a very simply website for this, Open Visual Studio 2013 -> New -> Web Site -> ASP.NET Empty Website. I have named it as ClientMSMQ.

STEP 7: Right click on ClientMSMQ from Solution explorer and Add -> Add New Item ->Web Form.

STEP 8: Add the following code in Default.aspx.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="https://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Button ID="Button1" runat="server" Text="Send message to the queue" OnClick ="Button1_Click"/>

<br/>

<br/>

<asp:Button ID="Button2" runat="server" Text="Read Message from the queue" OnClick="Button2_Click" />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</div>

</form>

</body>

</html>

Step 9 : Reference the service in the ClientMSMQ project. Right click on ClientMSMQ in solution explorer ->Add Service Reference. Now Put the service URL in the top box and click Go. Then click ok. Your service has been referenced.

STEP 11 : Make an object of the service and access the methods. Use this (encircled in the picture below) to make objects. Also, pot the following code on the Deafult.aspx.cs.

Code to put in Default.aspx.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using ServiceReference1;

public partial class _Default : System.Web.UI.Page

{

Service1Client client = new Service1Client();

protected void Button1_Click(object sender, EventArgs e)

{

client.SendToQueue();

}

protected void Button2_Click(object sender, EventArgs e)

{

int msg = 0;

msg = client.ReadFromQueue();

TextBox1.Text = msg.ToString();

}

}

STEP 12 : Press F5 to run the ClientMSMQ Website. it should look like this.

Step 13 : Click on first button - Send message to the queue. This should send a message to our queue. Click refresh on Public Queue folder and see of there is a message.

There! we have a message!

STEP 14 : Since we have hardcoded the value to 50 in our service, if we read the message from the queue the textbox should show the value 50. Lets check it!

There you have it!

 

Hope you enjoyed reading it!

Comments