//******************************************************************************************************
// Service.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/eclipse-1.0.php
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 11/15/2011 - Pinal C. Patel
// Generated original version of source code.
// 12/20/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Net;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace GSF.ServiceModel
{
///
/// Defines helper methods related to WCF-based web services.
///
public static class Service
{
private const string HostedAspNetEnvironment = "System.ServiceModel.Activation.HostedAspNetEnvironment, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
///
/// Creates a based on the specified .
///
/// The URI that is used to determine the type of to be created.
/// A boolean value that indicated whether security is to be enabled on the .
/// An object if a valid is specified; otherwise null.
///
/// This list shows all valid address schemes that can be specified in the :
///
///
/// Address Scheme
/// Usage
///
/// -
/// http:// or http.soap11://
/// An of http.soap11://localhost:2929 will create an and update the to http://localhost:2929.
///
/// -
/// http.soap12://
/// An of http.soap12://localhost:2929 will create an and update the to http://localhost:2929.
///
/// -
/// http.duplex://
/// An of http.duplex://localhost:2929 will create an and update the to http://localhost:2929.
///
/// -
/// http.rest://
/// An of http.rest://localhost:2929 will create an and update the to http://localhost:2929.
///
/// -
/// net.tcp://
/// An of net.tcp://localhost:2929 will create an and leave the unchanged.
///
/// -
/// net.p2p://
/// An of net.p2p://localhost:2929 will create an and leave the unchanged.
///
/// -
/// net.pipe://
/// An of net.pipe://localhost:2929 will create an and leave the unchanged.
///
/// -
/// net.msmq://
/// An of net.msmq://localhost:2929 will create an and leave the unchanged.
///
///
///
/// The parameter value is ignored Mono deployments since security bindings are not implemented.
///
///
public static Binding CreateServiceBinding(ref string address, bool enableSecurity)
{
address = address.Trim();
if (string.IsNullOrEmpty(address))
return null;
int index = address.IndexOf("://");
string scheme = address.Substring(0, index >= 0 ? index : address.Length);
switch (scheme.ToLower())
{
case "http":
case "http.soap11":
// Format address.
address = address.Replace("http.soap11", "http");
// Create binding.
BasicHttpBinding soap11Binding = new BasicHttpBinding();
#if !MONO
if (enableSecurity)
{
// Enable security.
soap11Binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
soap11Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
}
else
{
// Disable security.
soap11Binding.Security.Mode = BasicHttpSecurityMode.None;
soap11Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
}
#endif
return soap11Binding;
case "http.soap12":
// Format address.
address = address.Replace("http.soap12", "http");
// Create binding.
WSHttpBinding soap12Binding = new WSHttpBinding();
#if !MONO
if (enableSecurity)
{
// Enable security.
soap12Binding.Security.Mode = SecurityMode.Transport;
soap12Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
}
else
{
// Disable security.
soap12Binding.Security.Mode = SecurityMode.None;
soap12Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
}
#endif
return soap12Binding;
case "http.duplex":
// Format address.
address = address.Replace("http.duplex", "http");
// Create binding.
WSDualHttpBinding duplexBinding = new WSDualHttpBinding();
#if !MONO
if (enableSecurity)
{
// Enable security.
duplexBinding.Security.Mode = WSDualHttpSecurityMode.Message;
}
else
{
// Disable security.
duplexBinding.Security.Mode = WSDualHttpSecurityMode.None;
}
#endif
return duplexBinding;
case "http.rest":
// Format address.
address = address.Replace("http.rest", "http");
// Create binding.
WebHttpBinding restBinding = new WebHttpBinding();
#if !MONO
if (enableSecurity)
{
// Enable security.
restBinding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
restBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
}
else
{
// Disable security.
restBinding.Security.Mode = WebHttpSecurityMode.None;
restBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
}
#endif
return restBinding;
case "net.tcp":
// Create binding.
NetTcpBinding tcpBinding = new NetTcpBinding();
#if !MONO
if (enableSecurity)
{
// Enable security.
tcpBinding.Security.Mode = SecurityMode.Transport;
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
}
else
{
// Disable sercurity.
tcpBinding.Security.Mode = SecurityMode.None;
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
}
#endif
return tcpBinding;
case "net.p2p":
// Create binding.
#pragma warning disable 612, 618
NetPeerTcpBinding p2pBinding = new NetPeerTcpBinding();
#if !MONO
if (enableSecurity)
{
// Enable security.
p2pBinding.Security.Mode = SecurityMode.Transport;
}
else
{
// Disable security.
p2pBinding.Security.Mode = SecurityMode.None;
}
#endif
return p2pBinding;
case "net.pipe":
// Create binding.
NetNamedPipeBinding pipeBinding = new NetNamedPipeBinding();
#if !MONO
if (enableSecurity)
{
// Enable security.
pipeBinding.Security.Mode = NetNamedPipeSecurityMode.Transport;
}
else
{
// Disable security.
pipeBinding.Security.Mode = NetNamedPipeSecurityMode.None;
}
#endif
return pipeBinding;
case "net.msmq":
// Create binding.
NetMsmqBinding msmqBinding = new NetMsmqBinding();
#if !MONO
if (enableSecurity)
{
// Enable security.
msmqBinding.Security.Mode = NetMsmqSecurityMode.Transport;
}
else
{
// Disable security.
msmqBinding.Security.Mode = NetMsmqSecurityMode.None;
}
#endif
return msmqBinding;
default:
return null;
}
}
///
/// Gets the security setting of the hosting environment (For example: IIS web site or virtual directory).
///
public static AuthenticationSchemes GetAuthenticationSchemes(Uri baseAddress)
{
Type type = Type.GetType(HostedAspNetEnvironment);
object instance = Activator.CreateInstance(type, true);
return (AuthenticationSchemes)instance.GetType().InvokeMember("GetAuthenticationSchemes", BindingFlags.InvokeMethod, null, instance, new object[] { baseAddress });
}
///
/// Gets the contract that the service implements.
///
/// of the service.
/// of the service contract if found, otherwise null.
public static Type GetServiceContract(Type serviceType)
{
// Find contract interface by service type name.
Type contract = serviceType.GetInterface("I" + serviceType.Name);
if (contract == null)
{
// Fall back to the first interface the service implements.
Type[] interfaces = serviceType.GetInterfaces();
if (interfaces.Length > 0)
contract = interfaces[0];
}
return contract;
}
}
}