Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

View File

@@ -0,0 +1,38 @@
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using System.Xml;
using System.Runtime.Serialization.Json;
class DataContractJsonSerializerOperationBehavior : DataContractSerializerOperationBehavior
{
bool alwaysEmitTypeInformation;
public DataContractJsonSerializerOperationBehavior(OperationDescription description, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
: base(description)
{
this.MaxItemsInObjectGraph = maxItemsInObjectGraph;
this.IgnoreExtensionDataObject = ignoreExtensionDataObject;
this.DataContractSurrogate = dataContractSurrogate;
this.alwaysEmitTypeInformation = alwaysEmitTypeInformation;
}
public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
{
return new DataContractJsonSerializer(type, name, knownTypes, this.MaxItemsInObjectGraph, this.IgnoreExtensionDataObject, this.DataContractSurrogate, alwaysEmitTypeInformation);
}
public override XmlObjectSerializer CreateSerializer(Type type, System.Xml.XmlDictionaryString name, System.Xml.XmlDictionaryString ns, IList<Type> knownTypes)
{
return new DataContractJsonSerializer(type, name, knownTypes, this.MaxItemsInObjectGraph, this.IgnoreExtensionDataObject, this.DataContractSurrogate, alwaysEmitTypeInformation);
}
}
}

View File

@@ -0,0 +1,73 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
#pragma warning disable 1634, 1691
namespace System.ServiceModel.Description
{
using System;
using System.Runtime.Serialization;
[DataContract]
public class JsonFaultDetail
{
ExceptionDetail exceptionDetail;
string exceptionType;
string message;
string stackTrace;
[DataMember(Name = "ExceptionDetail")]
public ExceptionDetail ExceptionDetail
{
get
{
return exceptionDetail;
}
set
{
exceptionDetail = value;
}
}
[DataMember(Name = "ExceptionType")]
public string ExceptionType
{
get
{
return exceptionType;
}
set
{
exceptionType = value;
}
}
[DataMember(Name = "Message")]
public string Message
{
get
{
return message;
}
set
{
message = value;
}
}
[DataMember(Name = "StackTrace")]
public string StackTrace
{
get
{
return stackTrace;
}
set
{
stackTrace = value;
}
}
}
}

View File

@@ -0,0 +1,261 @@
//------------------------------------------------------------------------------
// <copyright file="WCFServiceClientProxyGenerator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.Globalization;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
using System.Collections.Generic;
using System.Xml;
using System.Collections;
using System.Web.Script.Services;
internal class WCFServiceClientProxyGenerator : ClientProxyGenerator
{
const int MaxIdentifierLength = 511;
const string DataContractXsdBaseNamespace = @"http://schemas.datacontract.org/2004/07/";
const string DefaultCallbackParameterName = "callback";
private string path;
private ServiceEndpoint serviceEndpoint;
// Similar to proxy generation code in WCF System.Runtime.Serialization.CodeExporter
// to generate CLR namespace from DataContract namespace
private static void AddToNamespace(StringBuilder builder, string fragment)
{
if (fragment == null)
{
return;
}
bool isStart = true;
for (int i = 0; i < fragment.Length && builder.Length < MaxIdentifierLength; i++)
{
char c = fragment[i];
if (IsValid(c))
{
if (isStart && !IsValidStart(c))
{
builder.Append("_");
}
builder.Append(c);
isStart = false;
}
else if ((c == '.' || c == '/' || c == ':') && (builder.Length == 1
|| (builder.Length > 1 && builder[builder.Length - 1] != '.')))
{
builder.Append('.');
isStart = true;
}
}
}
protected override string GetProxyPath()
{
return this.path;
}
protected override string GetJsonpCallbackParameterName()
{
if (this.serviceEndpoint == null)
{
return null;
}
WebMessageEncodingBindingElement webEncodingBindingElement = this.serviceEndpoint.Binding.CreateBindingElements().Find<WebMessageEncodingBindingElement>();
if (webEncodingBindingElement != null && webEncodingBindingElement.CrossDomainScriptAccessEnabled)
{
if (this.serviceEndpoint.Contract.Behaviors.Contains(typeof(JavascriptCallbackBehaviorAttribute)))
{
JavascriptCallbackBehaviorAttribute behavior = (JavascriptCallbackBehaviorAttribute)this.serviceEndpoint.Contract.Behaviors[typeof(JavascriptCallbackBehaviorAttribute)];
return behavior.UrlParameterName;
}
return DefaultCallbackParameterName;
}
return null;
}
protected override bool GetSupportsJsonp()
{
return !String.IsNullOrEmpty(GetJsonpCallbackParameterName());
}
private static Type ReplaceMessageWithObject(Type t)
{
return (typeof(Message).IsAssignableFrom(t)) ? typeof(object) : t;
}
static WebServiceData GetWebServiceData(ContractDescription contract)
{
WebServiceData serviceData = new WebServiceData();
//build method dictionary
Dictionary<string, WebServiceMethodData> methodDataDictionary = new Dictionary<string, WebServiceMethodData>();
// set service type
serviceData.Initialize(new WebServiceTypeData(XmlConvert.DecodeName(contract.Name), XmlConvert.DecodeName(contract.Namespace), contract.ContractType),
methodDataDictionary);
foreach (OperationDescription operation in contract.Operations)
{
Dictionary<string, WebServiceParameterData> parameterDataDictionary = new Dictionary<string, WebServiceParameterData>();
bool useHttpGet = operation.Behaviors.Find<WebGetAttribute>() != null;
WebServiceMethodData methodData = new WebServiceMethodData(serviceData, XmlConvert.DecodeName(operation.Name), parameterDataDictionary, useHttpGet);
// build parameter dictionary
MessageDescription requestMessage = operation.Messages[0];
if (requestMessage != null)
{
int numMessageParts = requestMessage.Body.Parts.Count;
for (int p = 0; p < numMessageParts; p++)
{
MessagePartDescription messagePart = requestMessage.Body.Parts[p];
// DevDiv 129964:JS proxy generation fails for a WCF service that uses an untyped message
// Message or its derived class are special, used for untyped operation contracts.
// As per the WCF team proxy generated for them should treat Message equivalent to Object type.
Type paramType = ReplaceMessageWithObject(messagePart.Type);
WebServiceParameterData parameterData = new WebServiceParameterData(XmlConvert.DecodeName(messagePart.Name), paramType, p);
parameterDataDictionary[parameterData.ParameterName] = parameterData;
serviceData.ProcessClientType(paramType, false, true);
}
}
if (operation.Messages.Count > 1)
{
// its a two way operation, get type information from return message
MessageDescription responseMessage = operation.Messages[1];
if (responseMessage != null)
{
if (responseMessage.Body.ReturnValue != null && responseMessage.Body.ReturnValue.Type != null)
{
// operation has a return type, add type to list of type proxy to generate
serviceData.ProcessClientType(ReplaceMessageWithObject(responseMessage.Body.ReturnValue.Type), false, true);
}
}
}
//add known types at operation level
for (int t = 0; t < operation.KnownTypes.Count; t++)
{
serviceData.ProcessClientType(operation.KnownTypes[t], false, true);
}
methodDataDictionary[methodData.MethodName] = methodData;
}
serviceData.ClearProcessedTypes();
return serviceData;
}
internal static string GetClientProxyScript(Type contractType, string path, bool debugMode, ServiceEndpoint serviceEndpoint)
{
ContractDescription contract = ContractDescription.GetContract(contractType);
WebServiceData webServiceData = GetWebServiceData(contract);
WCFServiceClientProxyGenerator proxyGenerator = new WCFServiceClientProxyGenerator(path, debugMode, serviceEndpoint);
return proxyGenerator.GetClientProxyScript(webServiceData);
}
// Similar to proxy generation code in WCF System.Runtime.Serialization.CodeExporter
// to generate CLR namespace from DataContract namespace
protected override string GetClientTypeNamespace(string ns)
{
if (string.IsNullOrEmpty(ns))
{
return String.Empty;
}
Uri uri = null;
StringBuilder builder = new StringBuilder();
if (Uri.TryCreate(ns, UriKind.RelativeOrAbsolute, out uri))
{
if (!uri.IsAbsoluteUri)
{
AddToNamespace(builder, uri.OriginalString);
}
else
{
string uriString = uri.AbsoluteUri;
if (uriString.StartsWith(DataContractXsdBaseNamespace, StringComparison.Ordinal))
{
AddToNamespace(builder, uriString.Substring(DataContractXsdBaseNamespace.Length));
}
else
{
string host = uri.Host;
if (host != null)
{
AddToNamespace(builder, host);
}
string path = uri.PathAndQuery;
if (path != null)
{
AddToNamespace(builder, path);
}
}
}
}
if (builder.Length == 0)
{
return String.Empty;
}
int length = builder.Length;
if (builder[builder.Length - 1] == '.')
{
length--;
}
length = Math.Min(MaxIdentifierLength, length);
return builder.ToString(0, length);
}
protected override string GetProxyTypeName(WebServiceData data)
{
return GetClientTypeNamespace(data.TypeData.TypeName);
}
// Similar to proxy generation code in WCF System.Runtime.Serialization.CodeExporter
// to generate CLR namespace from DataContract namespace
private static bool IsValid(char c)
{
UnicodeCategory uc = Char.GetUnicodeCategory(c);
// each char must be Lu, Ll, Lt, Lm, Lo, Nd, Mn, Mc, Pc
switch (uc)
{
case UnicodeCategory.UppercaseLetter: // Lu
case UnicodeCategory.LowercaseLetter: // Ll
case UnicodeCategory.TitlecaseLetter: // Lt
case UnicodeCategory.ModifierLetter: // Lm
case UnicodeCategory.OtherLetter: // Lo
case UnicodeCategory.DecimalDigitNumber: // Nd
case UnicodeCategory.NonSpacingMark: // Mn
case UnicodeCategory.SpacingCombiningMark: // Mc
case UnicodeCategory.ConnectorPunctuation: // Pc
return true;
default:
return false;
}
}
// Similar to proxy generation code in WCF System.Runtime.Serialization.CodeExporter
// to generate CLR namespace from DataContract namespace
private static bool IsValidStart(char c)
{
return (Char.GetUnicodeCategory(c) != UnicodeCategory.DecimalDigitNumber);
}
internal WCFServiceClientProxyGenerator(string path, bool debugMode, ServiceEndpoint serviceEndpoint)
{
this.path = path;
_debugMode = debugMode;
this.serviceEndpoint = serviceEndpoint;
}
}
}

View File

@@ -0,0 +1,71 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Web;
[DebuggerDisplay("Address={address}")]
[DebuggerDisplay("Name={name}")]
public class WebHttpEndpoint : WebServiceEndpoint
{
static Type WebHttpEndpointType = typeof(WebHttpEndpoint);
public WebHttpEndpoint(ContractDescription contract) :
this(contract, null /* address */)
{ }
public WebHttpEndpoint(ContractDescription contract, EndpointAddress address)
: base(contract, address)
{
this.Behaviors.Add(new WebHttpBehavior());
}
public bool HelpEnabled
{
get { return this.WebHttpBehavior.HelpEnabled; }
set { this.WebHttpBehavior.HelpEnabled = value; }
}
public WebMessageFormat DefaultOutgoingResponseFormat
{
get { return this.WebHttpBehavior.DefaultOutgoingResponseFormat; }
set { this.WebHttpBehavior.DefaultOutgoingResponseFormat = value; }
}
public bool AutomaticFormatSelectionEnabled
{
get { return this.WebHttpBehavior.AutomaticFormatSelectionEnabled; }
set { this.WebHttpBehavior.AutomaticFormatSelectionEnabled = value; }
}
public bool FaultExceptionEnabled
{
get { return this.WebHttpBehavior.FaultExceptionEnabled; }
set { this.WebHttpBehavior.FaultExceptionEnabled = value; }
}
WebHttpBehavior WebHttpBehavior
{
get
{
WebHttpBehavior webHttpBehavior = this.Behaviors.Find<WebHttpBehavior>();
if (webHttpBehavior == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.WebBehaviorNotFoundWithEndpoint, typeof(WebHttpEndpoint).Name, typeof(WebHttpBehavior).Name)));
}
return webHttpBehavior;
}
}
protected override Type WebEndpointType
{
get { return WebHttpEndpointType; }
}
}
}

View File

@@ -0,0 +1,124 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.Globalization;
using System.Net;
using System.Runtime.Serialization.Json;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Web;
class WebScriptClientGenerator : ServiceMetadataExtension.IHttpGetMetadata
{
internal const string DebugMetadataEndpointSuffix = "jsdebug";
internal const string MetadataEndpointSuffix = "js";
bool debugMode;
ServiceEndpoint endpoint;
NameValueCache<string> proxyCache;
DateTime serviceLastModified;
string serviceLastModifiedRfc1123String;
bool crossDomainScriptAccessEnabled;
public WebScriptClientGenerator(ServiceEndpoint endpoint, bool debugMode, bool crossDomainScriptAccessEnabled)
{
this.endpoint = endpoint;
this.debugMode = debugMode;
// The service host is automatically restarted every time a service or any of its dependencies change
// A restart adds all the behaviors from scratch.
// => WebScriptEnablingBehavior plugs in this contract for the "/js" endpoint afresh if the service changes.
this.serviceLastModified = DateTime.UtcNow;
// Zero out all millisecond and sub-millisecond information because RFC1123 doesn't support milliseconds.
// => The parsed If-Modified-Since date, against which serviceLastModified will need to be compared,
// won't have milliseconds.
this.serviceLastModified = new DateTime(this.serviceLastModified.Year, this.serviceLastModified.Month, this.serviceLastModified.Day, this.serviceLastModified.Hour, this.serviceLastModified.Minute, this.serviceLastModified.Second, DateTimeKind.Utc);
this.proxyCache = new NameValueCache<string>();
this.crossDomainScriptAccessEnabled = crossDomainScriptAccessEnabled;
}
string GetProxyContent(Uri baseUri)
{
string proxy = this.proxyCache.Lookup(baseUri.Authority);
if (String.IsNullOrEmpty(proxy))
{
proxy = WCFServiceClientProxyGenerator.GetClientProxyScript(this.endpoint.Contract.ContractType, baseUri.AbsoluteUri, this.debugMode, endpoint);
this.proxyCache.AddOrUpdate(baseUri.Authority, proxy);
}
return proxy;
}
string ServiceLastModifiedRfc1123String
{
get
{
if (serviceLastModifiedRfc1123String == null)
{
// "R" for RFC1123. The HTTP standard requires that dates be serialized according to RFC1123.
serviceLastModifiedRfc1123String = serviceLastModified.ToString("R", DateTimeFormatInfo.InvariantInfo);
}
return serviceLastModifiedRfc1123String;
}
}
public Message Get(Message message)
{
HttpRequestMessageProperty requestMessageProperty = (HttpRequestMessageProperty) message.Properties[HttpRequestMessageProperty.Name];
HttpResponseMessageProperty responseMessageProperty = new HttpResponseMessageProperty();
if ((requestMessageProperty != null) && IsServiceUnchanged(requestMessageProperty.Headers[JsonGlobals.IfModifiedSinceString]))
{
Message responseMessage = Message.CreateMessage(MessageVersion.None, string.Empty);
responseMessageProperty.StatusCode = HttpStatusCode.NotModified;
responseMessage.Properties.Add(HttpResponseMessageProperty.Name, responseMessageProperty);
return responseMessage;
}
string proxyContent = this.GetProxyContent(UriTemplate.RewriteUri(this.endpoint.Address.Uri, requestMessageProperty.Headers[HttpRequestHeader.Host]));
Message response = new WebScriptMetadataMessage(string.Empty, proxyContent);
responseMessageProperty.Headers.Add(JsonGlobals.LastModifiedString, ServiceLastModifiedRfc1123String);
responseMessageProperty.Headers.Add(JsonGlobals.ExpiresString, ServiceLastModifiedRfc1123String);
if (AspNetEnvironment.Current.AspNetCompatibilityEnabled)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
}
else
{
responseMessageProperty.Headers.Add(JsonGlobals.CacheControlString, JsonGlobals.publicString);
}
response.Properties.Add(HttpResponseMessageProperty.Name, responseMessageProperty);
return response;
}
internal static string GetMetadataEndpointSuffix(bool debugMode)
{
if (debugMode)
{
return DebugMetadataEndpointSuffix;
}
else
{
return MetadataEndpointSuffix;
}
}
bool IsServiceUnchanged(string ifModifiedSinceHeaderValue)
{
if (string.IsNullOrEmpty(ifModifiedSinceHeaderValue))
{
return false;
}
DateTime ifModifiedSinceDateTime;
if (DateTime.TryParse(ifModifiedSinceHeaderValue, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out ifModifiedSinceDateTime))
{
return (ifModifiedSinceDateTime >= serviceLastModified);
}
return false;
}
}
}

View File

@@ -0,0 +1,491 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
#pragma warning disable 1634, 1691
namespace System.ServiceModel.Description
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Security;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using System.Xml;
public sealed class WebScriptEnablingBehavior : WebHttpBehavior
{
static readonly DataContractJsonSerializer jsonFaultSerializer = new DataContractJsonSerializer(typeof(JsonFaultDetail));
static readonly WebMessageBodyStyle webScriptBodyStyle = WebMessageBodyStyle.WrappedRequest;
static readonly WebMessageFormat webScriptDefaultMessageFormat = WebMessageFormat.Json;
const int MaxMetadataEndpointBufferSize = 2048;
WebMessageFormat requestMessageFormat = webScriptDefaultMessageFormat;
WebMessageFormat responseMessageFormat = webScriptDefaultMessageFormat;
public WebScriptEnablingBehavior()
{
}
public override WebMessageBodyStyle DefaultBodyStyle
{
get
{
return webScriptBodyStyle;
}
set
{
if (value != webScriptBodyStyle)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.BodyStyleNotSupportedByWebScript, value, this.GetType().Name, webScriptBodyStyle)));
}
}
}
public override WebMessageFormat DefaultOutgoingRequestFormat
{
get
{
return this.requestMessageFormat;
}
set
{
if (!WebMessageFormatHelper.IsDefined(value))
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
}
this.requestMessageFormat = value;
}
}
public override WebMessageFormat DefaultOutgoingResponseFormat
{
get
{
return this.responseMessageFormat;
}
set
{
if (!WebMessageFormatHelper.IsDefined(value))
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
}
this.responseMessageFormat = value;
}
}
public override bool HelpEnabled
{
get
{
return false;
}
set
{
if (value)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.HelpPageNotSupportedInScripts)));
}
}
}
public override bool AutomaticFormatSelectionEnabled
{
get
{
return false;
}
set
{
if (value)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.AutomaticFormatSelectionNotSupportedInScripts)));
}
}
}
public override bool FaultExceptionEnabled
{
get
{
return false;
}
set
{
if (value)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.FaultExceptionEnabledNotSupportedInScripts)));
}
}
}
public override void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
base.ApplyClientBehavior(endpoint, clientRuntime);
#pragma warning disable 56506 // [....], clientRuntime.MessageInspectors is never null
clientRuntime.MessageInspectors.Add(new JsonClientMessageInspector());
#pragma warning restore 56506
}
public override void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
base.ApplyDispatchBehavior(endpoint, endpointDispatcher);
try
{
AddMetadataEndpoint(endpoint, endpointDispatcher, false); // debugMode
AddMetadataEndpoint(endpoint, endpointDispatcher, true); // debugMode
}
catch (XmlException exception)
{
// [....], need to reference this resource string although fix for 13332 was removed
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.InvalidXmlCharactersInNameUsedWithPOSTMethod, string.Empty, string.Empty, string.Empty), exception));
}
}
public override void Validate(ServiceEndpoint endpoint)
{
base.Validate(endpoint);
#pragma warning disable 56506 // [....], endpoint.Contract is never null
foreach (OperationDescription operation in endpoint.Contract.Operations)
#pragma warning restore 56506
{
if (operation.Behaviors.Find<XmlSerializerOperationBehavior>() != null)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR2.GetString(SR2.WebScriptNotSupportedForXmlSerializerFormat, typeof(XmlSerializerFormatAttribute).Name, this.GetType().ToString())));
}
string method = WebHttpBehavior.GetWebMethod(operation);
if (method != WebHttpBehavior.GET
&& method != WebHttpBehavior.POST)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR2.GetString(SR2.WebScriptInvalidHttpRequestMethod, operation.Name,
endpoint.Contract.Name, method, this.GetType().ToString())));
}
WebGetAttribute webGetAttribute = operation.Behaviors.Find<WebGetAttribute>();
if (webGetAttribute != null && webGetAttribute.UriTemplate != null)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR2.GetString(SR2.WebScriptNotSupportedForXmlSerializerFormat, typeof(UriTemplate).Name, this.GetType().ToString())));
}
WebInvokeAttribute webInvokeAttribute = operation.Behaviors.Find<WebInvokeAttribute>();
if (webInvokeAttribute != null && webInvokeAttribute.UriTemplate != null)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR2.GetString(SR2.WebScriptNotSupportedForXmlSerializerFormat, typeof(UriTemplate).Name, this.GetType().ToString())));
}
WebMessageBodyStyle bodyStyle = GetBodyStyle(operation);
if (bodyStyle != webScriptBodyStyle)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.BodyStyleNotSupportedByWebScript, bodyStyle, this.GetType().Name, webScriptBodyStyle)));
}
foreach (MessageDescription messageDescription in operation.Messages)
{
if (!messageDescription.IsTypedMessage &&
(messageDescription.Direction == MessageDirection.Output) &&
(messageDescription.Body.Parts.Count > 0))
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR2.GetString(SR2.WebScriptOutRefOperationsNotSupported, operation.Name,
endpoint.Contract.Name)));
}
}
}
}
internal override DataContractJsonSerializerOperationFormatter CreateDataContractJsonSerializerOperationFormatter(OperationDescription od, DataContractSerializerOperationBehavior dcsob, bool isWrapped)
{
return new DataContractJsonSerializerOperationFormatter(od, dcsob.MaxItemsInObjectGraph, dcsob.IgnoreExtensionDataObject, dcsob.DataContractSurrogate, isWrapped, true, this.JavascriptCallbackParameterName);
}
internal override string GetWmiTypeName()
{
return "WebScriptEnablingBehavior";
}
internal override bool UseBareReplyFormatter(WebMessageBodyStyle style, OperationDescription operationDescription, WebMessageFormat responseFormat, out Type parameterType)
{
if (responseFormat == WebMessageFormat.Json)
{
parameterType = null;
return false;
}
return base.UseBareReplyFormatter(style, operationDescription, responseFormat, out parameterType);
}
protected override void AddClientErrorInspector(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new JsonClientMessageInspector());
}
protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
if (endpointDispatcher.ChannelDispatcher == null)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(
"endpointDispatcher", SR2.GetString(SR2.ChannelDispatcherMustBePresent));
}
#pragma warning disable 56506 // [....], endpointDispatcher.ChannelDispatcher.ErrorHandlers never null
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new JsonErrorHandler(endpoint, endpointDispatcher.ChannelDispatcher.IncludeExceptionDetailInFaults));
#pragma warning restore 56506
}
protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
{
return new JsonQueryStringConverter(operationDescription);
}
void AddMetadataEndpoint(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher, bool debugMode)
{
Uri baseAddress = endpoint.Address.Uri;
if (baseAddress == null)
{
return;
}
ServiceHostBase host = endpointDispatcher.ChannelDispatcher.Host;
UriBuilder builder = new UriBuilder(baseAddress);
builder.Path += builder.Path.EndsWith("/", StringComparison.OrdinalIgnoreCase)
? (WebScriptClientGenerator.GetMetadataEndpointSuffix(debugMode))
: ("/" + WebScriptClientGenerator.GetMetadataEndpointSuffix(debugMode));
EndpointAddress metadataAddress = new EndpointAddress(builder.Uri);
foreach (ServiceEndpoint serviceEndpoint in host.Description.Endpoints)
{
if (EndpointAddress.UriEquals(serviceEndpoint.Address.Uri, metadataAddress.Uri, true, false))// ignoreCase // includeHostNameInComparison
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new InvalidOperationException(SR2.GetString(SR2.JsonNoEndpointAtMetadataAddress, this.GetType().ToString(), serviceEndpoint.Address, serviceEndpoint.Name, host.Description.Name)));
}
}
HttpTransportBindingElement transportBindingElement;
HttpTransportBindingElement existingTransportBindingElement = endpoint.Binding.CreateBindingElements().Find<HttpTransportBindingElement>();
if (existingTransportBindingElement != null)
{
transportBindingElement = (HttpTransportBindingElement)existingTransportBindingElement.Clone();
}
else
{
if (baseAddress.Scheme == "https")
{
transportBindingElement = new HttpsTransportBindingElement();
}
else
{
transportBindingElement = new HttpTransportBindingElement();
}
}
transportBindingElement.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
transportBindingElement.TransferMode = TransferMode.Buffered;
transportBindingElement.MaxBufferSize = MaxMetadataEndpointBufferSize;
transportBindingElement.MaxReceivedMessageSize = MaxMetadataEndpointBufferSize;
Binding metadataBinding = new CustomBinding(
new WebScriptMetadataMessageEncodingBindingElement(),
transportBindingElement);
BindingParameterCollection parameters = host.GetBindingParameters(endpoint);
// build endpoint dispatcher
ContractDescription metadataContract = ContractDescription.GetContract(typeof(ServiceMetadataExtension.IHttpGetMetadata));
OperationDescription metadataOperation = metadataContract.Operations[0];
EndpointDispatcher metadataEndpointDispatcher = new EndpointDispatcher(metadataAddress, metadataContract.Name, metadataContract.Namespace);
DispatchOperation dispatchOperation = new DispatchOperation(metadataEndpointDispatcher.DispatchRuntime, metadataOperation.Name, metadataOperation.Messages[0].Action, metadataOperation.Messages[1].Action);
dispatchOperation.Formatter = new WebScriptMetadataFormatter();
dispatchOperation.Invoker = new SyncMethodInvoker(metadataOperation.SyncMethod);
metadataEndpointDispatcher.DispatchRuntime.Operations.Add(dispatchOperation);
metadataEndpointDispatcher.DispatchRuntime.SingletonInstanceContext = new InstanceContext(host, new WebScriptClientGenerator(endpoint, debugMode, !String.IsNullOrEmpty(this.JavascriptCallbackParameterName)));
metadataEndpointDispatcher.DispatchRuntime.InstanceContextProvider = new SingletonInstanceContextProvider(metadataEndpointDispatcher.DispatchRuntime);
// build channel dispatcher
IChannelListener<IReplyChannel> listener = null;
if (metadataBinding.CanBuildChannelListener<IReplyChannel>(parameters))
{
listener = metadataBinding.BuildChannelListener<IReplyChannel>(metadataAddress.Uri, parameters);
}
ChannelDispatcher metadataChannelDispatcher = new ChannelDispatcher(listener);
metadataChannelDispatcher.MessageVersion = MessageVersion.None;
metadataChannelDispatcher.Endpoints.Add(metadataEndpointDispatcher);
host.ChannelDispatchers.Add(metadataChannelDispatcher);
}
class JsonClientMessageInspector : WebFaultClientMessageInspector
{
public override void AfterReceiveReply(ref Message reply, object correlationState)
{
bool callBase = true;
if (reply != null)
{
object responseProperty = reply.Properties[HttpResponseMessageProperty.Name];
if (responseProperty != null)
{
if (((HttpResponseMessageProperty)responseProperty).Headers[JsonGlobals.jsonerrorString] == JsonGlobals.trueString)
{
callBase = false;
XmlDictionaryReader reader = reply.GetReaderAtBodyContents();
JsonFaultDetail faultDetail = jsonFaultSerializer.ReadObject(reader) as JsonFaultDetail;
FaultCode faultCode = new FaultCode(FaultCodeConstants.Codes.InternalServiceFault, FaultCodeConstants.Namespaces.NetDispatch);
faultCode = FaultCode.CreateReceiverFaultCode(faultCode);
if (faultDetail != null)
{
if (faultDetail.ExceptionDetail != null)
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new FaultException<ExceptionDetail>(faultDetail.ExceptionDetail, faultDetail.Message, faultCode));
}
else
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new FaultException(MessageFault.CreateFault(faultCode, faultDetail.Message)));
}
}
else
{
throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new FaultException(MessageFault.CreateFault(faultCode,
System.ServiceModel.SR.GetString(System.ServiceModel.SR.SFxInternalServerError))));
}
}
}
}
if (callBase)
{
base.AfterReceiveReply(ref reply, correlationState);
}
}
}
class JsonErrorHandler : IErrorHandler
{
bool includeExceptionDetailInFaults;
string outgoingContentType;
public JsonErrorHandler(ServiceEndpoint endpoint, bool includeExceptionDetailInFaults)
{
WebMessageEncodingBindingElement webMEBE = endpoint.Binding.CreateBindingElements().Find<WebMessageEncodingBindingElement>();
outgoingContentType = JsonMessageEncoderFactory.GetContentType(webMEBE);
this.includeExceptionDetailInFaults = includeExceptionDetailInFaults;
}
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
HttpResponseMessageProperty responseProperty;
if (fault == null)
{
FaultCode code = new FaultCode(FaultCodeConstants.Codes.InternalServiceFault, FaultCodeConstants.Namespaces.NetDispatch);
code = FaultCode.CreateReceiverFaultCode(code);
string action = FaultCodeConstants.Actions.NetDispatcher;
MessageFault innerFault;
innerFault = MessageFault.CreateFault(code, new FaultReason(error.Message, CultureInfo.CurrentCulture), new ExceptionDetail(error));
fault = Message.CreateMessage(version, action, new JsonFaultBodyWriter(innerFault, this.includeExceptionDetailInFaults));
responseProperty = new HttpResponseMessageProperty();
fault.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);
}
else
{
MessageFault innerFault = MessageFault.CreateFault(fault, TransportDefaults.MaxFaultSize);
Message newMessage = Message.CreateMessage(version, fault.Headers.Action, new JsonFaultBodyWriter(innerFault, this.includeExceptionDetailInFaults));
newMessage.Headers.To = fault.Headers.To;
newMessage.Properties.CopyProperties(fault.Properties);
object property = null;
if (newMessage.Properties.TryGetValue(HttpResponseMessageProperty.Name, out property))
{
responseProperty = (HttpResponseMessageProperty)property;
}
else
{
responseProperty = new HttpResponseMessageProperty();
newMessage.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);
}
fault.Close();
fault = newMessage;
}
responseProperty.Headers.Add(HttpResponseHeader.ContentType, outgoingContentType);
responseProperty.Headers.Add(JsonGlobals.jsonerrorString, JsonGlobals.trueString);
responseProperty.StatusCode = System.Net.HttpStatusCode.InternalServerError;
object bodyFormatPropertyObject;
if (fault.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatPropertyObject))
{
WebBodyFormatMessageProperty bodyFormatProperty = bodyFormatPropertyObject as WebBodyFormatMessageProperty;
if ((bodyFormatProperty == null) ||
(bodyFormatProperty.Format != WebContentFormat.Json))
{
fault.Properties[WebBodyFormatMessageProperty.Name] = WebBodyFormatMessageProperty.JsonProperty;
}
}
else
{
fault.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty);
}
}
class JsonFaultBodyWriter : BodyWriter
{
JsonFaultDetail faultDetail;
public JsonFaultBodyWriter(MessageFault fault, bool includeExceptionDetailInFaults)
: base(false)
{
faultDetail = new JsonFaultDetail();
if (includeExceptionDetailInFaults)
{
faultDetail.Message = fault.Reason.ToString();
if (fault.HasDetail)
{
try
{
ExceptionDetail originalFaultDetail = fault.GetDetail<ExceptionDetail>();
faultDetail.StackTrace = originalFaultDetail.StackTrace;
faultDetail.ExceptionType = originalFaultDetail.Type;
faultDetail.ExceptionDetail = originalFaultDetail;
}
catch (SerializationException exception)
{
System.ServiceModel.DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
// A SerializationException will be thrown if the detail isn't of type ExceptionDetail
// In that case, we want to just move on.
}
catch (SecurityException exception)
{
System.ServiceModel.DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information);
// A SecurityException will be thrown if the detail can't be obtained in partial trust
// (This is guaranteed to happen unless there's an Assert for MemberAccessPermission, since ExceptionDetail
// has DataMembers that have private setters.)
// In that case, we want to just move on.
}
}
}
else
{
faultDetail.Message = System.ServiceModel.SR.GetString(System.ServiceModel.SR.SFxInternalServerError);
}
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
jsonFaultSerializer.WriteObject(writer, faultDetail);
}
}
}
}
}

View File

@@ -0,0 +1,53 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.Globalization;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceModel.Web;
using System.Text;
using System.Xml;
[DebuggerDisplay("Address={address}")]
[DebuggerDisplay("Name={name}")]
public class WebScriptEndpoint : WebServiceEndpoint
{
static Type WebScriptEndpointType = typeof(WebScriptEndpoint);
public WebScriptEndpoint(ContractDescription contract) :
this(contract, null /* address */)
{ }
public WebScriptEndpoint(ContractDescription contract, EndpointAddress address)
: base(contract, address)
{
this.Behaviors.Add(new WebScriptEnablingBehavior());
}
WebScriptEnablingBehavior webScriptEnablingBehavior
{
get
{
WebScriptEnablingBehavior webScriptEnablingBehavior = this.Behaviors.Find<WebScriptEnablingBehavior>();
if (webScriptEnablingBehavior == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.WebBehaviorNotFoundWithEndpoint, WebEndpointType.Name, typeof(WebScriptEnablingBehavior).Name)));
}
return webScriptEnablingBehavior;
}
}
protected override Type WebEndpointType
{
get { return WebScriptEndpointType; }
}
}
}

View File

@@ -0,0 +1,98 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Xml;
using System.ServiceModel.Web;
public abstract class WebServiceEndpoint : ServiceEndpoint
{
internal WebServiceEndpoint(ContractDescription contract, EndpointAddress address)
: base(contract, new WebHttpBinding(), address)
{ }
public HostNameComparisonMode HostNameComparisonMode
{
get { return this.webHttpBinding.HostNameComparisonMode; }
set { this.webHttpBinding.HostNameComparisonMode = value; }
}
public long MaxBufferPoolSize
{
get { return this.webHttpBinding.MaxBufferPoolSize; }
set { this.webHttpBinding.MaxBufferPoolSize = value; }
}
public int MaxBufferSize
{
get { return this.webHttpBinding.MaxBufferSize; }
set { this.webHttpBinding.MaxBufferSize = value; }
}
public long MaxReceivedMessageSize
{
get { return this.webHttpBinding.MaxReceivedMessageSize; }
set { this.webHttpBinding.MaxReceivedMessageSize = value; }
}
public TransferMode TransferMode
{
get { return this.webHttpBinding.TransferMode; }
set { this.webHttpBinding.TransferMode = value; }
}
public XmlDictionaryReaderQuotas ReaderQuotas
{
get { return this.webHttpBinding.ReaderQuotas; }
set { this.webHttpBinding.ReaderQuotas = value; }
}
public WebHttpSecurity Security
{
get { return this.webHttpBinding.Security; }
}
public Encoding WriteEncoding
{
get { return this.webHttpBinding.WriteEncoding; }
set { this.webHttpBinding.WriteEncoding = value; }
}
public WebContentTypeMapper ContentTypeMapper
{
get { return this.webHttpBinding.ContentTypeMapper; }
set { this.webHttpBinding.ContentTypeMapper = value; }
}
public bool CrossDomainScriptAccessEnabled
{
get { return this.webHttpBinding.CrossDomainScriptAccessEnabled; }
set
{
this.webHttpBinding.CrossDomainScriptAccessEnabled = value;
}
}
protected abstract Type WebEndpointType { get; }
WebHttpBinding webHttpBinding
{
get
{
WebHttpBinding webHttpBinding = this.Binding as WebHttpBinding;
if (webHttpBinding == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR2.GetString(SR2.WebHttpBindingNotFoundWithEndpoint, WebEndpointType.Name, typeof(WebHttpBinding).Name)));
}
return webHttpBinding;
}
}
}
}