win10系统,添加完消息队列组件之后,写了个控制台的WCF MSMQ小例子。
运行服务端总是报错,试过网上能查到的配置文件还有代码的方式修改了,还是报那个错误。
“System.InvalidOperationException”类型的未经处理的异常在 System.ServiceModel.dll 中发生
其他信息: 打开队列时出错。确保已安装和运行 MSMQ,队列存在并且具有正确的读取权限。内部异常可能包含更多信息。
请问有么没人给点思路处理一下这个问题?
契约接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibrary1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract(IsOneWay=true)]
void writeFile(string content);
}
}
契约实现类
namespace WcfServiceLibrary1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
public class Service1 : IService1
{
[OperationBehavior(TransactionScopeRequired=true,TransactionAutoComplete=true)]
public void writeFile(string content)
{
// 实现写文件的功能
StreamWriter sw = new StreamWriter("c:\\wcfMSMQ.txt", true);
sw.Write(content);
sw.WriteLine();
sw.Close();
}
}
}
服务端控制台代码
// 创建队列
string queueName = @".\private$\myQueue"; // 看命名格式,私有的就是专用的
// 如果队列不存在,则创建
if (!MessageQueue.Exists(queueName))
{
MessageQueue.Create(queueName, true); // 创建事务性的队列
}
// host服务
ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
if (host.State != CommunicationState.Opened)
{
host.Open();
}
Console.WriteLine("服务已启动!");
Console.ReadLine();
}
服务端APP.config代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- 部署服务库项目时,必须将配置文件的内容添加到
主机的 app.config 文件中。System.Configuration 不支持库的配置文件。 -->
<system.serviceModel>
<bindings>
<netMsmqBinding>
<binding name="binding1">
<security>
<transport msmqAuthenticationMode="None" msmqProtectionLevel="None"/>
<message clientCredentialType="UserName"/>
</security>
</binding>
</netMsmqBinding>
</bindings>
<services>
<service name="WcfServiceLibrary1.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://127.0.0.1:3300/msmq" />
<add baseAddress="net.msmq://127.0.0.1/private/myQueue"/>
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
<endpoint address="" binding="netMsmqBinding" bindingConfiguration="binding1" contract="WcfServiceLibrary1.IService1">
<!--
部署时,应删除或替换下列标识元素,以反映
用来运行所部署服务的标识。删除之后,WCF 将
自动推断相应标识。
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 -->
<!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除 -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,
请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="True"/>
<!-- 要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false
以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>