1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Security;
using System.ServiceModel.Security;
[DebuggerDisplay("Name={name}, Namespace={ns}, ContractType={contractType}")]
public class ContractDescription
{
Type callbackContractType;
string configurationName;
Type contractType;
XmlName name;
string ns;
OperationDescriptionCollection operations;
SessionMode sessionMode;
KeyedByTypeCollection<IContractBehavior> behaviors = new KeyedByTypeCollection<IContractBehavior>();
ProtectionLevel protectionLevel;
bool hasProtectionLevel;
public ContractDescription(string name)
: this(name, null)
{
}
public ContractDescription(string name, string ns)
{
// the property setter validates given value
this.Name = name;
if (!string.IsNullOrEmpty(ns))
NamingHelper.CheckUriParameter(ns, "ns");
this.operations = new OperationDescriptionCollection();
this.ns = ns ?? NamingHelper.DefaultNamespace; // ns can be ""
}
internal string CodeName
{
get { return this.name.DecodedName; }
}
[DefaultValue(null)]
public string ConfigurationName
{
get { return this.configurationName; }
set { this.configurationName = value; }
}
public Type ContractType
{
get { return this.contractType; }
set { this.contractType = value; }
}
public Type CallbackContractType
{
get { return this.callbackContractType; }
set { this.callbackContractType = value; }
}
public string Name
{
get { return this.name.EncodedName; }
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
if (value.Length == 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new ArgumentOutOfRangeException("value", SR.GetString(SR.SFxContractDescriptionNameCannotBeEmpty)));
}
this.name = new XmlName(value, true /*isEncoded*/);
}
}
public string Namespace
{
get { return this.ns; }
set
{
if (!string.IsNullOrEmpty(value))
NamingHelper.CheckUriProperty(value, "Namespace");
this.ns = value;
}
}
public OperationDescriptionCollection Operations
{
get { return this.operations; }
}
public ProtectionLevel ProtectionLevel
{
get { return this.protectionLevel; }
set
{
if (!ProtectionLevelHelper.IsDefined(value))
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
this.protectionLevel = value;
this.hasProtectionLevel = true;
}
}
public bool ShouldSerializeProtectionLevel()
{
return this.HasProtectionLevel;
}
public bool HasProtectionLevel
{
get { return this.hasProtectionLevel; }
}
[DefaultValue(SessionMode.Allowed)]
public SessionMode SessionMode
{
get { return this.sessionMode; }
set
{
if (!SessionModeHelper.IsDefined(value))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
}
this.sessionMode = value;
}
}
public KeyedCollection<Type, IContractBehavior> ContractBehaviors
{
get { return this.Behaviors; }
}
[EditorBrowsable(EditorBrowsableState.Never)]
public KeyedByTypeCollection<IContractBehavior> Behaviors
{
get { return this.behaviors; }
}
public static ContractDescription GetContract(Type contractType)
{
if (contractType == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractType");
TypeLoader typeLoader = new TypeLoader();
return typeLoader.LoadContractDescription(contractType);
}
public static ContractDescription GetContract(Type contractType, Type serviceType)
{
if (contractType == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractType");
if (serviceType == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceType");
TypeLoader typeLoader = new TypeLoader();
ContractDescription description = typeLoader.LoadContractDescription(contractType, serviceType);
return description;
}
public static ContractDescription GetContract(Type contractType, object serviceImplementation)
{
if (contractType == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractType");
if (serviceImplementation == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceImplementation");
TypeLoader typeLoader = new TypeLoader();
Type serviceType = serviceImplementation.GetType();
ContractDescription description = typeLoader.LoadContractDescription(contractType, serviceType, serviceImplementation);
return description;
}
public Collection<ContractDescription> GetInheritedContracts()
{
Collection<ContractDescription> result = new Collection<ContractDescription>();
for (int i = 0; i < Operations.Count; i++)
{
OperationDescription od = Operations[i];
if (od.DeclaringContract != this)
{
ContractDescription inheritedContract = od.DeclaringContract;
if (!result.Contains(inheritedContract))
{
result.Add(inheritedContract);
}
}
}
return result;
}
internal void EnsureInvariants()
{
if (string.IsNullOrEmpty(this.Name))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.AChannelServiceEndpointSContractSNameIsNull0)));
}
if (this.Namespace == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.AChannelServiceEndpointSContractSNamespace0)));
}
if (this.Operations.Count == 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxContractHasZeroOperations, this.Name)));
}
bool thereIsAtLeastOneInitiatingOperation = false;
for (int i = 0; i < this.Operations.Count; i++)
{
OperationDescription operationDescription = this.Operations[i];
operationDescription.EnsureInvariants();
if (operationDescription.IsInitiating)
thereIsAtLeastOneInitiatingOperation = true;
if ((!operationDescription.IsInitiating || operationDescription.IsTerminating)
&& (this.SessionMode != SessionMode.Required))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.ContractIsNotSelfConsistentItHasOneOrMore2, this.Name)));
}
}
if (!thereIsAtLeastOneInitiatingOperation)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFxContractHasZeroInitiatingOperations, this.Name)));
}
}
internal bool IsDuplex()
{
for (int i = 0; i < this.operations.Count; ++i)
{
if (this.operations[i].IsServerInitiated())
{
return true;
}
}
return false;
}
}
}
|