0

以下是我的服务代码

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.ServiceModel.Channels;
using System.Xml;

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

        public Message GetData()
        {
            // Create body
            string body = ("<test>data</test>");

            // Create messatge
            MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
            Message msg = Message.CreateMessage(ver, "ResponseToGetDataRequest", body);

            //Debug.WriteLine(msg.ToString());
            return msg;

        }

    }

    class TestDataWriter : BodyWriter
    {
        string _data;

        public TestDataWriter(string data)
            : base(false)
        {
            _data = data;
        }

        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            writer.WriteRaw(_data);
        }
    }

}

这是合同

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.ServiceModel.Channels;

namespace WcfServiceRaw
{

    [ServiceContract]
    public interface IService1
    {

        [OperationContract(ReplyAction = "ResponseToGetDataRequest")]
        Message GetData();
        //[OperationContract(ReplyAction = "ResponseToGetDataRequest")]
        //Message GetData1();
    }

}

最后我提到了我的我们配置代码

<?xml version="1.0"?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true"></trace>
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true" >
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="myUserTraceSource" switchValue="Information, ActivityTracing">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml" type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="C:\logs\TracesBinding.svclog" />
    </sharedListeners>
  </system.diagnostics>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
 <!-- <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>

          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>-->
  <system.serviceModel>
        <services>
      <service name="WcfServiceRaw.Service1" behaviorConfiguration="MyServiceTypeBehaviors" >
        <!-- Service Endpoints -->
        <endpoint
          address="http://localhost:49389/Service1.svc" binding="wsHttpBinding" contract="WcfServiceRaw.IService1">
        </endpoint>
        <endpoint address="mex"  binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors" >
          <serviceMetadata httpGetEnabled="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

客户端代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using System.Xml;

namespace WcfClientRaw
{
    class Program
    {
        static void Main(string[] args)
        {
            //IMyContract proxy = ChannelFactory<IMyContract>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(address));
            WcfServiceRaw.Service1Client proxy = new WcfServiceRaw.Service1Client();
            using (proxy as IDisposable)
            {
                Message msg = proxy.GetData();
                Console.WriteLine(msg.ToString());
                Console.WriteLine();

                XmlDictionaryReader xdr = msg.GetReaderAtBodyContents();
                //string exp = "<test>data</test>";
                string act = xdr.ReadOuterXml();
                //Debug.Assert(exp == act);
                Console.WriteLine(act);
                Console.ReadLine();
            }
        }
    }
}

和应用程序配置是

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <!-- <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:49389/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="WcfServiceRaw.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>-->

    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:49389/Service1.svc" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IService1" contract="WcfServiceRaw.IService1"
                name="WSHttpBinding_IService1">
                <identity>
                    <userPrincipalName value="sheraz.akbar@curemd.com" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

现在我得到了上述异常,你能帮帮我吗,跟踪日志也显示没有消息通信。

4

0 回答 0