2014-08-30 26 views
1

我想第一次運行MSMQ服務。郵件沒有被添加到我的MSMQ

我已經從MSDN複製了一個示例,我試圖讓它工作。

一切運行正常,沒有錯誤。但是當我去檢查我的MSMQ時,沒有消息可用/添加。

我已經使用了跟蹤工具SvcTraceViewe.exe,並且沒有錯誤報告。

這是一類DLL中定義我的服務:

public class MotionSaver : IMotionSaver 
{ 
    [OperationBehavior] 
    public void MotionFrame(byte[] data) 
    { 
     // DO I NEED TO PUT ANTHING IN HERE?? 
    } 
} 

[ServiceContract] 
public interface IMotionSaver 
{ 
    //IsOneWay=true denotes that there will no return message from the server to the client. 
    [OperationContract(IsOneWay = true)] 
    void MotionFrame(byte[] data); 
} 

這是調用上面的DLL我Server主機:

static void Main(string[] args) 
{ 
    try 
    { 
     using (ServiceHost host = new ServiceHost(typeof(MotionSaver))) 
     { 
      //Path of the Queue. Here we are creating a private queue with the name VishalQ 
      //where all our message would be stored, if server is unavialable. 
      string queueName = ConfigurationManager.AppSettings["queueName"]; 
      //Checking whether the queue exists or not. 
      if (!MessageQueue.Exists(queueName)) 
      { 
       //If the queue doesnot exists it will create a queue with the name VishalQ 
       //the second parameter false denotes that the queue would be a non transaction queue. If you want that your queue to be 
       //transaction then make the second parameter to true. 
       MessageQueue.Create(queueName, true); 
      } 
      //finally opening the host to server the clients. 
      host.Open(); 
      Console.WriteLine("Server is up and running on port 32578"); 
      Console.WriteLine("Press any key to exit"); 
      Console.ReadKey(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.ToString()); 
     Console.ReadKey(); 
    } 
} 

這是在主機應用我的app.config設置:

<configuration> 
    <appSettings> 
    <add key="queueName" value=".\private$\MotionSaverTest4" /> 
    </appSettings> 

    <system.diagnostics> 
    <trace autoflush="true" /> 
    <sources> 
     <source name="System.ServiceModel" 
       switchValue="Information, ActivityTracing" 
       propagateActivity="true"> 
     <listeners> 
      <add name="sdt" 
       type="System.Diagnostics.XmlWriterTraceListener" 
       initializeData= "SdrConfigExample.e2e" /> 
     </listeners> 
     </source> 
    </sources> 
    </system.diagnostics> 


    <system.serviceModel> 
    <diagnostics performanceCounters="All"/> 
    <services> 
     <service name="InformedMotion.Motion.MotionSaver" behaviorConfiguration="myBehavior"> 
     <!--Address attribute specifies the name of the MSMQ Queue.--> 
     <endpoint name="motionQ" address="net.msmq://localhost/private/MotionSaverTest4" binding="netMsmqBinding" 
        bindingConfiguration="myMSMQ"     
        contract="InformedMotion.Motion.IMotionSaver"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.msmq://localhost/private/"/> 
      <!--Both Mex and HttpBinding uses http://localhost:8888 port--> 
      <add baseAddress="http://localhost:32578"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <bindings> 
     <!--The property exactlyOnce=false means that i am using non transactional queue. The property is by default true.--> 
     <netMsmqBinding> 
     <binding name="myMSMQ" exactlyOnce="true"> 
     <!--<binding name="myMSMQ" exactlyOnce="false" durable="false">--> 
      <!-- 
         If we donot set the security mode to none then the following error occurs. 
         Binding validation failed because the binding's MsmqAuthenticationMode property is set to 
         WindowsDomain but MSMQ is installed with Active Directory integration disabled. 
         The channel factory or service host cannot be opened. 
        --> 
      <security mode="None"/> 
     </binding> 
     </netMsmqBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="myBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!--This is for enabling an exception--> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/></startup></configuration> 

這是調用服務的客戶端應用程序:

static void Main(string[] args) 
{ 
    wsMotionQ.MotionSaverClient ws = new MotionSaverClient(); 
    ws.MotionFrame(new byte[] { 1 }); 

    Console.WriteLine("All Wishes sent successfully"); 
    Console.ReadLine(); 
} 

,這是我的客戶的app.config:

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <netMsmqBinding> 
       <binding name="motionQ"> 
        <security mode="None" /> 
       </binding> 
      </netMsmqBinding> 
     </bindings> 
     <client> 
      <endpoint address="net.msmq://localhost/private/MotionSaverTest4" 
       binding="netMsmqBinding" bindingConfiguration="motionQ" contract="wsMotionQ.IMotionSaver" 
       name="motionQ"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
     </client> 
    </system.serviceModel> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/> 
    </startup> 
</configuration> 

我檢查我的消息隊列已創建。

1)。如何將消息添加到隊列中?我的服務中有一個空白方法的意義是什麼? 2)。爲什麼我的郵件沒有被添加?

謝謝

N.B.

更改代碼這樣:

[OperationBehavior] 
public void MotionFrame(byte[] jpegData) 
{ 
    using (Message msg = new Message()) 
    { 
     msg.BodyStream = new MemoryStream(jpegData); 
     msgQMissedData.Send(msg); 
    } 
} 

回答

1

你只是創建隊列,但你不能從中發送/接收消息/。

http://www.codeproject.com/Articles/5830/Using-MSMQ-from-C

+0

嗨,我跟着這個格蘭例如:http://www.c-sharpcorner.com/UploadFile/17e8f6/msmq-in-wcf/。它在該函數中也沒有代碼。我在學習期待有代碼還是應該使用常識,我需要添加代碼大聲笑? – 2014-08-30 12:43:32

+0

嗨,我提出了我的問題,包括該代碼,但仍然無法正常工作... – 2014-08-30 12:55:48

+0

嗨,我當然需要在我的服務中添加代碼添加代碼(LOL)。我終於通過手動創建MSMQ而不是通過代碼來實現它 - 這使它工作... – 2014-08-30 14:04:54