开发者

Two endpoint (soap ,json) and one service method

开发者 https://www.devze.com 2023-04-03 22:22 出处:网络
I have that service [OperationContract] [WebGet(UriTemplate = \"/GetData\")] List<FieldInfo> GetSerializedData();

I have that service

        [OperationContract]
    [WebGet(UriTemplate = "/GetData")]
    List<FieldInfo> GetSerializedData();

and web.config

<system.web>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
    <httpRuntime  executionTimeout="90" maxRequestLength="1048576" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100"/>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingSettings" maxBufferPoolSize="524288" maxReceivedMessageSize="654321" sendTimeout="00:10:00" closeTimeout="00:01:00" openTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </webHttpBinding>
      <wsHttpBinding>
        <binding name="wsHttpBindingSettings" maxBufferPoolSize="524288" maxReceivedMessageSize="654321" sendTimeout="00:10:00" closeTimeout="00:01:00" openTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="MetadataBehavior" name="ServiceModel.Service">
        <endpoint name="soap" address="soap" behaviorConfiguration="Default" binding="wsHttpBinding"
          bindingConfiguration="wsHttpBindingSettings" contract="ServiceModel.IService" />
        <endpoint name="Json" address="json" behaviorConfiguration="JSON" binding="webHttpBinding"
        bindingConfiguration="webHttpBindingSettings" contract="ServiceModel.IService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://service.com/Service.svc/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MetadataBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="JSON">
          <webHttp automaticFormatSelectionEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="10000000"/>
        </behavior>
        <behavior name="Default">
          <dataContractSerializer maxItemsInObjectGraph="10000000"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

Why on the client side only one endpoint is generated ?

    <client>
  <endpoint address="http://service.renault.com/Service.svc/soap"
    binding="wsHttpBinding" bindingConfiguration="soap" contract="ServiceReference1.IService"
    name="soap" />
</client>

My point is to execute service method from asp.net page codebehind and wcf return data in soap or json depends on ContentType. But how to set in asp.net page content type to applicati开发者_高级运维on/json when it have text/html content. I have problem with understand it.


Why on the client side only one endpoint is generated?

Because WCF does not emit metadata for non-SOAP endpoints. Unlike WSDL and MEX for SOAP, there's no widely used metadata format for "REST" endpoints (WADL is one of them, but it's not much used and not implemented by WCF), so on the Add Service Reference (or svcutil) will only see one endpoint in the metadata and only that one will be created.

I want to use WCF feature which select proper serialization type depends on ContentType of request

JSON vs XML is a serialization type decision; JSON vs SOAP is not (SOAP is a well-defined protocol, with rules for what the request should look like) - see more information on WCF Dynamic Response Format. Your webHttBinding-endpoint will do that (return JSON or XML based on the incoming request), since you enabled auto format selection, but the way you'd consume this service doesn't need to be with a WCF client - using WebClient, HttpWebRequest should work out just fine.


If possible, try to design you Visual Studio like this :

  • Solution
    • project with contracts (only the IXXXXService)
    • web project with the implementation and all the endpoints (reference the contract project)
    • clients project not using the VS generated proxy, but a factory that can select the correct endpoint and so protocol. (reference the contract project)

Here is a sample class I use in a scenario similar to yours :

public class ServiceHelper
{

    /// <summary>
    /// WCF proxys do not clean up properly if they throw an exception. This method ensures that the service 
    /// proxy is handeled correctly. Do not call TService.Close() or TService.Abort() within the action lambda.
    /// </summary>
    /// <typeparam name="TService">The type of the service to use</typeparam>
    /// <param name="action">Lambda of the action to performwith the service</param>
    [System.Diagnostics.DebuggerStepThrough]
    public static void UsingProxy<TService>(Action<TService> action)
        where TService : ICommunicationObject, IDisposable, new()
    {
        var service = new TService();
        bool success = false;
        try
        {
            action(service);
            if (service.State != CommunicationState.Faulted)
            {
                service.Close();
                success = true;
            }
        }
        finally
        {
            if (!success)
            {
                service.Abort();
            }
        }
    }
    /// <summary>
    /// WCF proxys do not clean up properly if they throw an exception. This method ensures that the service 
    /// proxy is handeled correctly. Do not call TService.Close() or TService.Abort() within the action lambda.
    /// </summary>
    /// <typeparam name="TIServiceContract">The type of the service contract to use</typeparam>
    /// <param name="action">Action to perform with the client instance.</param>
    /// <remarks>In the configuration, an endpoint with names that maches the <typeparamref name="TIServiceContract"/> name
    /// must exists. Otherwise, use <see cref="UsingContract&lt;TIServiceContract&gt;(string endpointName, Action<TIServiceContract> action)"/>. </remarks>
    [System.Diagnostics.DebuggerStepThrough]
    public static void UsingContract<TIServiceContract>(Action<TIServiceContract> action)
    {
        UsingContract<TIServiceContract>(
            typeof(TIServiceContract).Name,
            action
            );
    }
    /// <summary>
    /// WCF proxys do not clean up properly if they throw an exception. This method ensures that the service 
    /// proxy is handeled correctly. Do not call TService.Close() or TService.Abort() within the action lambda.
    /// </summary>
    /// <typeparam name="TIServiceContract">The type of the service contract to use</typeparam>
    /// <param name="action">Action to perform with the client instance.</param>
    /// <param name="endpointName">Name of the endpoint to use</param>
    [System.Diagnostics.DebuggerStepThrough]
    public static void UsingContract<TIServiceContract>(
          string endpointName,
          Action<TIServiceContract> action)
    {
        var cf = new ChannelFactory<TIServiceContract>(endpointName);
        var channel = cf.CreateChannel();
        var clientChannel = (IClientChannel)channel;

        bool success = false;
        try
        {
            action(channel);
            if (clientChannel.State != CommunicationState.Faulted)
            {
                clientChannel.Close();
                success = true;
            }
        }
        finally
        {
            if (!success) clientChannel.Abort();
        }
    }
}

In the client config, I set up manually my references :

<system.serviceModel>
<client>
  <endpoint address="http://localhost/myapp/myservice.svc/soap"
            binding="wsHttpBinding"
            contract="MyProject.Contracts.IMyService"
            name="IMyServiceSoap"/>
  <endpoint address="http://localhost/myapp/myservice.svc/rest"
            binding="webHttpBinding"
            contract="MyProject.Contracts.IMyService"
            name="IMyServiceRest"/>

</client>
</system.serviceModel>

Then, in your code you can simply call :

        ServiceHelper.UsingContract<"IMyServiceSoap", MyProject.Contracts.IMyService>(
            svc => svc.DoTheJob()
            );

or

        ServiceHelper.UsingContract<"IMyServiceRest", MyProject.Contracts.IMyService>(
            svc => svc.DoTheJob()
            );

[edit] The server config is similar to this one :

<services>
<service name="MyService">
    <endpoint address="soap"
    binding="wsHttpBinding"
                        contract="MyContracts.IMyService"/>
    <endpoint address="rest"
    binding="webHttpBinding"
                        contract="MyContracts.IMyService"/>
    <endpoint address="mex"
                        binding="mexHttpBinding"
                        contract="IMetadataExchange"/>
</service>
</services>
0

精彩评论

暂无评论...
验证码 换一张
取 消