WCF CustomBing exception - AddressingNone does not support adding WS-Addressing headers - c#

I keep getting the following exception when I use my pragmatically created CustomBinding.
Addressing Version 'AddressingNone (http://schemas.microsoft.com/ws/2005/05/addressing/none)' does not support adding WS-Addressing headers.
Is there any way to resolve this issue?
private static CustomBinding CreateCustomBinding(bool useHttps)
{
BindingElement security;
BindingElement transport;
if (useHttps)
{
security = SecurityBindingElement.CreateSecureConversationBindingElement(
SecurityBindingElement.CreateUserNameOverTransportBindingElement());
transport = new HttpsTransportBindingElement
{
MaxBufferPoolSize = 2147483647,
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
};
}
else
{
security = SecurityBindingElement.CreateSecureConversationBindingElement(
SecurityBindingElement.CreateUserNameForSslBindingElement(true));
transport = new HttpTransportBindingElement
{
MaxBufferPoolSize = 2147483647,
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
};
}
var encoding = new MtomMessageEncodingBindingElement
{
MaxReadPoolSize = 64,
MaxWritePoolSize = 16,
MaxBufferSize = 2147483647,
MessageVersion = MessageVersion.Soap11,
WriteEncoding = System.Text.Encoding.UTF8
};
//var encoding = new TextMessageEncodingBindingElement();
var customBinding = new CustomBinding();
customBinding.Elements.Add(security);
customBinding.Elements.Add(encoding);
customBinding.Elements.Add(transport);
return customBinding;
}

Just in case anyone is interested, below is my solution. I added an if for dealing with the text encoding section and also updated the SecurityBindingElement under if (useHttps):
private static CustomBinding CreateCustomBinding(bool useHttps, bool textEncoding)
{
BindingElement security;
BindingElement encoding;
BindingElement transport;
if (useHttps)
{
var seq = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
seq.MessageSecurityVersion =
MessageSecurityVersion.
WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
seq.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
seq.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Default;
security = seq;
transport = new HttpsTransportBindingElement
{
MaxBufferPoolSize = 2147483647,
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
};
}
else
{
security = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
transport = new HttpTransportBindingElement
{
MaxBufferPoolSize = 2147483647,
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
};
}
if (textEncoding)
encoding = new TextMessageEncodingBindingElement
{
MaxReadPoolSize = 64,
MaxWritePoolSize = 16,
MessageVersion = MessageVersion.Soap11,
WriteEncoding = System.Text.Encoding.UTF8
};
else
encoding = new MtomMessageEncodingBindingElement
{
MaxReadPoolSize = 64,
MaxWritePoolSize = 16,
MaxBufferSize = 2147483647,
MessageVersion = MessageVersion.Soap11,
WriteEncoding = System.Text.Encoding.UTF8
};
var customBinding = new CustomBinding();
customBinding.Elements.Add(security);
customBinding.Elements.Add(encoding);
customBinding.Elements.Add(transport);
return customBinding;
}

It worked for me with small changes. Thanks Tawani. Just I set AuthenticationSheme to Basic for HTTPSTransportBinidingElement.
transport = new HttpsTransportBindingElement
{
MaxBufferPoolSize = 2147483647,
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
AuthenticationScheme = System.Net.AuthenticationSchemes.Basic
};

Related

WCF client failed when receive bytes array over 350 MB

When I receive bytes array over 350 MB, I am getting error as "The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."
Client side config
var binding = new NetTcpBinding(SecurityMode.None)
{
PortSharingEnabled = true,
MaxBufferSize = Int32.MaxValue,
MaxReceivedMessageSize = Int32.MaxValue,
ReaderQuotas = new XmlDictionaryReaderQuotas
{
MaxArrayLength = Int32.MaxValue,
MaxBytesPerRead = Int32.MaxValue,
MaxDepth = Int32.MaxValue,
MaxNameTableCharCount = Int32.MaxValue,
MaxStringContentLength = Int32.MaxValue,
},
MaxBufferPoolSize = 0,
TransactionFlow = false,
TransactionProtocol = TransactionProtocol.Default,
TransferMode = TransferMode.Streamed,
OpenTimeout = new TimeSpan(0, 10, 0),
CloseTimeout = new TimeSpan(0, 10, 0),
SendTimeout = new TimeSpan(0, 10, 0),
ReceiveTimeout = new TimeSpan(0, 10, 0)
};
container.Register(Component.For<IFtpsServiceFacade>()
.AsWcfClient(
new DefaultClientModel(
WcfEndpoint.BoundTo(binding)
.At(string.Format("net.tcp://{0}/FileService.Ftps",
"localhost")))
));
Server side config.
var binding = new NetTcpBinding
{
PortSharingEnabled = true,
MaxBufferSize = Int32.MaxValue,
MaxReceivedMessageSize = Int32.MaxValue,
ReaderQuotas = new XmlDictionaryReaderQuotas
{
MaxArrayLength = Int32.MaxValue,
MaxBytesPerRead = Int32.MaxValue,
MaxDepth = Int32.MaxValue,
MaxNameTableCharCount = Int32.MaxValue,
MaxStringContentLength = Int32.MaxValue,
},
MaxBufferPoolSize = 0,
TransactionFlow = false,
TransactionProtocol = TransactionProtocol.Default,
TransferMode = TransferMode.Buffered,
OpenTimeout = new TimeSpan(0, 10, 0),
CloseTimeout = new TimeSpan(0, 10, 0),
SendTimeout = new TimeSpan(0, 10, 0),
ReceiveTimeout = new TimeSpan(0, 10, 0)
};
container.Register(Component.For<IFtpsServiceFacade>()
.ImplementedBy<FtpsServiceFacade>()
.AsWcfService(
new DefaultServiceModel()
.AddEndpoints(
WcfEndpoint.BoundTo(binding)
.At(string.Format("net.tcp://{0}/FileService.Ftps",
"localhost"))
)).LifestyleSingleton() );
The establishment of the communication channel between the server-side and the client-side requires that the server-side and the client-side have the same binding settings. Therefore, the above error occurred. In order to solve this problem, we should change the settings on the server-side instead of the client-side. The max size of the supported file above 2GB, finished by the below configuration.
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
binding.MaxBufferSize = Int32.MaxValue;
binding.MaxBufferPoolSize = Int32.MaxValue;
binding.MaxReceivedMessageSize = Int32.MaxValue;
Especially to the security mode, it should be fully consistent with the setting located on the client-side.
Feel free to let me know if the problem still exists.

Set ServiceBehaviorAttribute in WCF Client?

I need to set by code the parameter
ServiceBehaviorAttribute
private static BasicHttpBinding getBinding()//BasicHttpBinding getBinding()
{
//WSHttpBinding binding = new WSHttpBinding();
//WSHttpBinding binding = new WSHttpBinding();
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
binding.ReceiveTimeout = new TimeSpan(8, 0, 0);
binding.SendTimeout = new TimeSpan(8, 0, 0);
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
binding.MaxBufferPoolSize = int.MaxValue;
binding.ReaderQuotas.MaxDepth = 64;
binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
return binding;
}
private static EndpointAddress getEndPoint()
{
EndpointAddress endPoint = new EndpointAddress(HTTP_SERVER);
return endPoint;
}
ConnectionToServer = new ConnectionToServer (getBinding(), new EndpointAddress(HTTP_SERVER));
Hot to insert in the ConnectionToServer this code ???
ServiceBehaviorAttribute sba = new ServiceBehaviorAttribute();
sba.MaxItemsInObjectGraph = int.MaxValue;
One thing is the endpoint configuration (i.e. the code you posted) and another completely different thing is the service behavior.
To set SBA.MaxItemsInObjectGraph you need to specify it in the execution behavior of the service contract which is done via a ServiceBehaviorAttribute in the WCF Service (not the client as your code implies).
i.e:
[ServiceBehavior(
InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Reentrant,
MaxItemsInObjectGraph = 34)]
public class WcfService : IDuplexService
{
//service implementation goes here
}
Here's how you can set MaxItemsInObjectGraph on a ChannelFactory:
DuplexChannelFactory<IService> cf = new DuplexChannelFactory<IService>(typeof(ServiceCallback), Server.ServerBinding(), ep);
foreach (OperationDescription operation in cf.Endpoint.Contract.Operations)
{
var dc = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dc != null)
{
dc.MaxItemsInObjectGraph = int.MaxValue;
}
}

Sharepoint 2010 GetListItems with Webservice - ListSoapClient with DefaultNetworkCredentials

I want to get some ListItems of a SharePoint 2010 List with a Win8 App. Everything works fine when I set the Credentials manually like:
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
EndpointAddress endpoint = new EndpointAddress("http://site1/site2/_vti_bin/Lists.asmx");
string listName = "{4e661b3b-d0a9-4440-b98f-3f3ef41a44a7}";
string viewName = "{f1ba8d46-ad36-40ef-b4bc-6f74ea87b5d7}";
string rowLimit = "25";
XElement ndQuery = new XElement("Query");
XElement ndViewFields = new XElement("ViewFields");
XElement ndQueryOptions = new XElement("QueryOptions");
MyService.ListsSoapClient client = new MyService.ListsSoapClient(basicHttpBinding, endpoint);
client.ClientCredentials.Windows.ClientCredential.UserName = "user";
client.ClientCredentials.Windows.ClientCredential.Password = "pw";
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
MyService.GetListItemsResponse response = await client.GetListItemsAsync(listName, viewName, ndQuery, ndViewFields, rowLimit, ndQueryOptions, null);
If I try to set the Credentials with the logged on Windows user i get the following Unauthorized error:
The HTTP request is unauthorized with client authentication scheme
'Negotiate'. The authentication header received from the server was
'Negotiate,NTLM'.
client.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
Can you help me?
try this (use default credentials instead of specific):
private ListsSoapClient CreateListsSoapClient(string siteUrl, string siteUserName, string sitePassword)
{
var basicHttpBinding = new BasicHttpBinding
{
CloseTimeout = new TimeSpan(00, 5, 00),
OpenTimeout = new TimeSpan(00, 5, 00),
ReceiveTimeout = new TimeSpan(00, 5, 00),
SendTimeout = new TimeSpan(00, 5, 00),
TextEncoding = Encoding.UTF8,
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue,
Security =
{
Mode = BasicHttpSecurityMode.TransportCredentialOnly
},
ReaderQuotas =
{
MaxArrayLength = int.MaxValue,
MaxBytesPerRead = int.MaxValue,
MaxStringContentLength = int.MaxValue
}
};
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
var url = string.Format("{0}/_vti_bin/Lists.asmx", siteUrl);
var address = new EndpointAddress(url);
var listsSoapClient = new ListsSoapClient(basicHttpBinding, address);
listsSoapClient.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Delegation;
listsSoapClient.ChannelFactory.Credentials.Windows.ClientCredential = new NetworkCredential(siteUserName, sitePassword);
listsSoapClient.ChannelFactory.Credentials.Windows.AllowNtlm = true;
return listsSoapClient;
}

How to correctly set security for NetNamedPipe?

I have a WCF service with NetNamedPipe for interprocess communication and I would like to add security on it. Everything works great without security, but when I am trying to use tranport security I am getting "InvalidCredentialException: The server has rejected the client credentials" exception. Can you please help me?
Code sample:
var netPipeBinding = new NetNamedPipeBinding() { MaxReceivedMessageSize = 2147483647, SendTimeout = TimeSpan.FromMinutes(10), ReceiveTimeout = TimeSpan.FromMinutes(10) };
netPipeBinding.ReaderQuotas.MaxDepth = 2147483647;
netPipeBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
netPipeBinding.ReaderQuotas.MaxArrayLength = 2147483647;
netPipeBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
netPipeBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
netPipeBinding.Security.Mode = NetNamedPipeSecurityMode.Transport;
netPipeBinding.Security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign;
var host = new ServiceHost(typeof(MainService));
var netPipeEA = new EndpointAddress(new Uri("net.pipe://MyProject/ServerSide"));
var contractDescription = ContractDescription.GetContract(typeof (IMainService), typeof (MainService));
host.AddServiceEndpoint(new ServiceEndpoint(contractDescription, netPipeBinding, netPipeEA));
host.Opened += HostOnOpened;
host.Open();
...
...
private void HostOnOpened(object sender, EventArgs eventArgs)
{
var netPipeBinding = new NetNamedPipeBinding() { MaxReceivedMessageSize = 2147483647, SendTimeout = TimeSpan.FromMinutes(10), ReceiveTimeout = TimeSpan.FromMinutes(10) };
netPipeBinding.ReaderQuotas.MaxDepth = 2147483647;
netPipeBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
netPipeBinding.ReaderQuotas.MaxArrayLength = 2147483647;
netPipeBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
netPipeBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
netPipeBinding.Security.Mode = NetNamedPipeSecurityMode.Transport;
netPipeBinding.Security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign;
DuplexChannelFactory<IMainService> channelFactory = new DuplexChannelFactory<IMainService>(new InstanceContext(new CalbackHandler()), netPipeBinding,
new EndpointAddress(IMainService));
var proxy = channelFactory.CreateChannel();
proxy.DoPing();
}
Thank you
The machine name, in this case "localhost" because you are using named pipe should be defined in the EndpointAddress URI.

Silverlight out of browser mode

Does silverlight support SSL in oob mode? I have SSL turned on and it works fine in browser, but it doesnt connect oob mode. Is there something I am missing?
Thanks
EDIT:
_serviceHost = new ServiceHost(typeof(ApplicationService), new Uri(BaseAddress));
_serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpsGetEnabled = true });
var serviceBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport)
{
MessageEncoding = WSMessageEncoding.Text,
TextEncoding = Encoding.UTF8,
BypassProxyOnLocal = false,
UseDefaultWebProxy = true,
CloseTimeout = new TimeSpan(10, 0, 0),
OpenTimeout = new TimeSpan(10, 0, 0),
SendTimeout = new TimeSpan(10, 0, 0),
ReceiveTimeout = new TimeSpan(10, 0, 0),
HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
MaxBufferPoolSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
AllowCookies = false,
TransferMode = TransferMode.StreamedResponse,
ReaderQuotas =
{
MaxDepth = 32,
MaxStringContentLength = int.MaxValue,
MaxArrayLength = 6553600,
MaxBytesPerRead = 4096,
MaxNameTableCharCount = 16384
}
};
var policyRetrieverBinding = new WebHttpBinding(WebHttpSecurityMode.Transport)
{
BypassProxyOnLocal = false,
UseDefaultWebProxy = true,
CloseTimeout = new TimeSpan(10, 0, 0),
OpenTimeout = new TimeSpan(10, 0, 0),
SendTimeout = new TimeSpan(10, 0, 0),
ReceiveTimeout = new TimeSpan(10, 0, 0),
HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
MaxBufferPoolSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
AllowCookies = false,
TransferMode = TransferMode.StreamedResponse,
ReaderQuotas =
{
MaxDepth = 32,
MaxStringContentLength = int.MaxValue,
MaxArrayLength = 6553600,
MaxBytesPerRead = 4096,
MaxNameTableCharCount = 16384
}
};
//service endpoint
_serviceHost.AddServiceEndpoint(typeof(IApplicationService), serviceBinding, ServiceEndPoint);
//mex endpoint for metadata
_serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpsBinding(), "mex");
//policy retriever endpoint
_serviceHost.AddServiceEndpoint(typeof(IPolicyRetriever), policyRetrieverBinding, string.Empty)
.Behaviors.Add(new WebHttpBehavior());
//so we get detailed exceptions
_serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>()
.IncludeExceptionDetailInFaults = true;
_serviceHost.Open();
The service endpoint is configured to use :
"https://" + Environment.MachineName

Categories

Resources