Using multiple DataContracts for one service - c#

I've got a ServiceContract that has an OperationContract with the following method signature: Manipulate(int fileid, param object[] operations).
I also have five DataContracts defined for the WCF service, and I'd like object[] operations to accept any number and combination of those. The problem is that they aren't visible unless I use them in the method signature. If I do that though, then only one type of DataContract can be used at a time, defeating the purpose of using an object[].
How do I make all five DataContracts visible on the client side without having to alter the method signature?

Use known types. For example in your config file you could define the different known types:
<configuration>
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<knownType type="SomeNs.Foo, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
<knownType type="SomeNs.Bar, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
<knownType type="SomeNs.Baz, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
</configuration>
Now clients will know about the Foo, Bar and Baz data contracts.
This being said, I would recommend you to use a common base type for your data contracts instead of object. Having a method signature that takes object as input is hard to understand from a consumer standpoint.

Related

Enterprise library exception: Activation error occured while trying to get instance of type ICacheManager, key?

Using .net 3.5 and Enterprise library 5.0.
From my research, I found a similar issue here:
Activation error occured while trying to get instance of type ICacheManager, key "Cache Manager" *** this solution did not fix my issue.
I can't seem to figure it out, my config should be set up correctly, but keep getting the exception? Anyone have similar issues?
I made the suggestion to add cacheManager and reference when I call the cache manager:
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
.....
....
ICacheManager cm = CacheFactory.GetCacheManager("TrackingCacheManager");
The App.config:
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<cachingConfiguration defaultCacheManager="TrackingCacheManager">
<cacheManagers>
<add name="TrackingCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
expirationPollFrequencyInSeconds="120" maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
</cacheManagers>
<backingStores>
<add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="NullBackingStore" />
</backingStores>
</cachingConfiguration>
<dataConfiguration defaultDatabase="ConnectionString" />
<connectionStrings>
<add name="ConnectionString" connectionString="server=AFS7BCBRNGQ5O0\DEVELOPMENT;database=EITC_RTS;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
My references:
My guess is that you've setup this configuration in an App.config for your Domain project. But your main project has it's own config file which this configuration must be copied into.
So for example, if your main project was a webapp, then it would have a web.config. The caching configuration you have added to the App.config of the Domain project is not used at runtime. The configuration being used is from main project's config, in this example the web.config.
Copy your Caching configuration from the Domain App.Config to the main config file and it will work.
I have done silly mistake, but after read above, understand the issue. This is my vb.net code which give me the above error.
Imports System.Collections
'Imports System.Configuration
Public Class DatabaseLogic
Public ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("db").ToString()
Public Function ServerDataMagic(StoredProcedure As String, PDMdata As Hashtable) As DataSet
Dim db As Database = DatabaseFactory.CreateDatabase(ConnectionString ) 'Here I am getting error.
Using cmd As DbCommand = db.GetStoredProcCommand(StoredProcedure)
Try
db.DiscoverParameters(cmd)
Catch discover_ex As Exception
End Try
and in web.config entry is
<add name="db" connectionString="Database=Dbname;Server=SERVER;uid=sa;pwd=sa#1234" providerName="System.Data.SqlClient" />
After read just get the issue is CreateDatabase method wants a config entry key as a string and I was given the exact connection string via config entry access. This is my updated code.
Dim db As Database = DatabaseFactory.CreateDatabase("db") 'Here I changed the config entry key
I was post to somebody help.

Getting a strange exception with the MS Exception Handling Block

I've created a very basic Logging block and Exception Handling block. In my code, I went to go test the Exception Handling (I know the Logging block works) with the following:
public void RunScriptClean()
{
try
{
throw new FileNotFoundException();
}
catch (FileNotFoundException ex)
{
var b = ExceptionPolicy.HandleException(ex, "Logging Policy");
if (b)
throw;
}
}
However, on the very first line of the catch block, I get this long winded exception and my application crashes:
Exception occured: The current build operating (build key Build Key [Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, Logging Policy]) failed: The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' cannot be resolved. Please verify the spelling is correct or that the full type name is provided. (Strategy type ConfiguredObjectStrategy, index 2).
I have absolutely no idea what it's referring to when it says the type cannot be resolved. I've added references to Microsoft.Practices.EnterpriseLibrary.Common/ExceptionHandling/Logging and Ms.Practices.ObjectBuilder2. This one class has using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling included at the top.
Added a screenshot of the configuration tool looking at my AppConfig file:
I'm sure I'm missing something basic, but it's tough to find a tutorial for EAB 4.1-- CodeProject has a lot for the original versions but I couldn't make much of them...
Edit I tried creating a new Formatter and naming it TextExceptionFormatter but that didn't change anything. Wasn't sure if maybe some how the FormatterType property on my Logging Handler was tied to that node.
And the actual block of XML from App.config:
<exceptionHandling>
<exceptionPolicies>
<add name="Logging Policy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" name="Exception">
<exceptionHandlers>
<add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Logging Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
I found this SO question: Cannot resolve type runtime error after registering the Enterprise Library into the GAC but even after changing the Version segment of the fullName attribute my app still behaves the same.
Alright, I was able to find a sample application that used the Logging handlers. Turns out I needed a reference to ExceptionHandling.Logging:
Microsoft.Practices.EnterpriseLibrary.Common
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging ****
Microsoft.Practices.EnterpriseLibrary.Logging
Microsoft.Practices.ObjectBuilder2
Where as I only had a references to:
Microsoft.Practices.EnterpriseLibrary.Common
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Microsoft.Practices.EnterpriseLibrary.Logging
Microsoft.Practices.ObjectBuilder2

Problems exposing a RIA services as SOAP, Json, etc

I'm trying to expose an existing RIA service with SOAP and JSON.
In the web.config, serviceModel section, I've put:
<system.serviceModel>
<domainServices>
<endpoints>
<add name="OData" type="System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory, System.ServiceModel.DomainServices.Hosting.OData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="Soap" type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="Json" type="Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</endpoints>
</domainServices>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
In MyDomainService there is:
[Query(IsDefault=true)]
public IEnumerable<UserItem> GetUsers()
{
return this.ObjectContext.Users;
}
I've tried the following URLs:
http://10.0.0.191:27070/Manager-Web-MyDomainService.svc/GetUsers
http://10.0.0.191:27070/Manager-Web-MyDomainService.svc/Soap/GetUsers
http://10.0.0.191:27070/Manager-Web-MyDomainService.svc/Json/GetUsers
http://10.0.0.191:27070/Manager-Web-MyDomainService.svc/OData/GetUsers
and I get just blank pages.
I've enabled tracing and in the log I see the warning "Configuration evaluation context not found".
Anybody who can help me with this?
Thanks in advance,
Cheers,
Gianluca.
Ok, I've sorted out almost everything. My configuration was correct. Problems were elsewhere. Let me share my findings:
First of all, I've found out that OData requires a '/' at the end of the URL. Also, I was wrongly thinking that it is necessary to recall the service URL with at the end the name of the method. Something like: http:///oData/. It turned out that by calling just http:///oData/ I was getting all the expected data.
At the contrary, Json does not want the trailing '/'. A correct URL is like: http:///Json/. This time it's been necessary to indicate the method. I'm also starting to understand better the meaning of the attributes Query, Invoke, etc. An INVOKE-decorated method is exposed as JSON only if it has the property HasSideEffect=false.
I am still having trouble exposing the same methods via SOAP. If someone wants to contribute and help me out, please feel free to add more info here. I'll post more information as soon as I get further results.
Cheers,
Gianluca.

Using a profile property of type List in .NET Membership

I'm working on a C# Webservice that needs to provide authentication as well as roles and profile management. I need each profile to have a property of type List. The profile section in the web.config looks like this:
<profile defaultProvider="MyProfileProvider" enabled="true">
<providers>
<remove name="MyProfileProvider"/>
<add connectionStringName="MySqlServer"
applicationName="MyApp"
name="MyProfileProvider"
type="System.Web.Profile.SqlProfileProvider" />
</providers>
<properties>
<add name="Websites" type="System.Collections.Generic.List<String>" serializeAs="Binary"/>
</properties>
</profile>
However, when I start the webservice and try to access that property it returns the following error:
System.Configuration.ConfigurationErrorsException: Attempting to load this property's type resulted in the following error: Could not load type 'System.Collections.Generic.List<String>'. (C:\Projects\MyProject\web.config line 58) ---> System.Web.HttpException: Could not load type 'System.Collections.Generic.List<String>'.
Is there a way to use a generic collection for this purpose?
After more searching I finally managed to find the answer. The solution is to use the type name qualified by its namespace. This means that for my issue I used:
<add name="Websites" type="System.Collections.Generic.List`1[System.String]" serializeAs="Binary"/>
I also found out that it is also possible to specify classes defined in other assemblies. To use those you need the assembly-qualified name. For example, that would be "System.Collections.Generic.HashSet`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" for a Hashset of Strings.

Unity Framework's container type's constructor param's type

Currently I am trying to use a config file to give Unity Framework information that looks like this...
<configuration>
<unity>
<typeAliases>
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity, Culture=neutral, Version=1.1.0.0, PublicKeyToken=31bf3856ad364e35" />
</typeAliases>
<containers>
<container>
<types>
<type type="Common.ISharedConfiguration, Common, Version=3.1.0.0, Culture=neutral, PublicKeyToken=1111111111111111" mapTo="Common.SharedConfigurationManager, Common, Version=3.1.0.0, Culture=neutral, PublicKeyToken=1111111111111111">
<lifetime type="singleton" />
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<constructor>
<param name="OurEnumChoice" parameterType="MyProjectsEnum" >
<value value="MyProjectsEnum.OurFirstConstant" type="MyProjectsEnum"/>
</param>
</constructor>
</typeConfig>
</type>
</types>
</container>
</containers>
</unity>
</configuration>
If I choose something like System.String and have my concrete class have a construtor of string this config file info for Unity works great. The moment I choose to use an Enum instead of string Unity throws an error like this...
Could not load type MyProjectsEnum from assembly Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf33856ad364e35
I think I need an understanding of what I can pass as an understood type by Unity beyond simple types through this configuration process.
You need to specify fully qualified type name for both 'parameterType' and 'type' attributes. Much the same you did for 'typeAlias' node. By default, Unity looks up its own assembly for unqualified types.

Categories

Resources