Creating a data contract assembly - wcf

Can anyone tell me how to make a data contract assembly?

Create a class library project in your Visual Studio solution
add the classes that contain the data contracts to that project
compile everything
reference that assembly from your service code and your client-side code
Such an assembly is just a regular class-library assembly - there's nothing magic or mysterious about it....

Use svcutil
svcutil /dataContractOnly {Service_Metadata_Location}
Add the generated code to a Library and compile
Suppose you have to generate code for Client Proxies which use the above DataContracts, use the /reference:{Assembly_Path} option of svcutil to reuse the types from the assembly.

Related

Replace COM DLL with new managed COM Callable assembly

Is there a smart way to scaffold a COM callable .NET class library from an existing native COM DLL?
Scenario
Suppose you have a COM based C++ Win32 application and you want to replace one of the COM DLLs with something written from scratch.
Constraints
The new library shall be written in C#, targeting the .NET Framework (4.x)
No modifications to the rest of the existing unmanaged application shall be required.
No recompilation of the unmanaged code shall be required.
What I already know
You can create .NET assemblies which are COM callable.
You can import the type library IDL from an existing COM DLL.
Based on this question, what I want should be possible, even if arduous.
Question
Is there a smart / efficient way to generate the scaffolding code for a COM callable .NET class library with the exact same signature as an existing unmanaged COM DLL so I can replace the DLLs?
There does not need to be any implementation at first, every method could just throw a NotImplementedException for example.

Where is the app.config

I have created a new project in Visual Studio 2013 (class library). There is no app.config. When I select add new item, there is no app.config in the list. Were is the app.config?
I have a ClassLibrary, which contains a number of NUnit Test classes. The problem is that there is lot of code like this throughout the application:
_ConString = ConfigurationManager.ConnectionStrings("GeniedbConnection").ConnectionString
Therefore when I run the tests I get a Null Pointer Exception. Is it possible to do this in the test classes:
ConfigurationManager.ConnectionStrings("GeniedbConnection").ConnectionString = "Connection String"
I get a Null Pointer Exception when I put the code on the above line into the test class. I believe it is because I am trying to set the variable before declaring it. Is that correct?
A class library doesn't contain an app config, since it isn't an app on it's own, but it can be an addition to an app. You will see an app config if you would create a winform application for example.
Class libraries are dll files that are frequently exchanged between different applications, these are often frequently used classes, that can speed up development, since you don't have to write code that you wrote once before.
MSDN Class Library
Edit:
You should use a UnitTest project and not a class library. Following question + answer will help you out, about using the .config file of your application: Use appliction config file from unittest project
UnitTest Project
If you add a reference to System.Configuration in your class library then you can use the following namespace to access the app.config of any applications which use it:
Imports System.Configuration.ConfigurationManager
...
Dim x As String = AppSettings("MySettingName")

Can I import a dll for one class only?

Good day,
I have used dll imports for "user32.dll" in the past.
However, I am trying to import a class library into my application which has some namespaces which come into conflict with namespaces which are already imported and referenced from other class libraries.
How can I reference this dll and only use the namespaces contained in it, or override the other namespaces imported from other class libraries in one class without it affecting the rest of the application.
I am still pretty new, this may not be possible.
Thank you.
To summarize the relevant issues:
In VB.NET one can use the Declare statement to call win32 functions in DLL's with typical EntryPoint constructs.
.NET Assemblies do not provide classical Win32 type EntryPoints (as such they cannot be 'declared')
If one needs to reference a .NET Assembly [or COM] you need to add a reference to the target library when compiling (usually done in the VS IDE or with the /r: switch)
In some cases Namespaces of such referenced Assemblies may collide with others. (i.E. referencing the same Assembly in different versions)
In that case one needs to import the required (conflicting) Namespaces with an Alias
For example:
Assuming you have an assembly with a root namespace Net that could collide with System.Net use:
Imports System
Imports ExtNet = SomeNetworkAssembly
Then in this case to access members of that assembly use ExtNet instead of Net
Note you can name the ExtNet part as you wish.
In C# one can do it via the using keyword instead.

Create C++ ATL COM nested namespaces like System namespace in .NET

I have several ATL COM services and would like each of them to have their own namespace, but be under a single base namespace, just like the System namespace in .NET.
For example if a C# project were to include these COM objects, all would be under the same base namespace:
using MyCompanyName.Terminator;
using MyCompanyName.Superman;
using MyCompanyName.Thor;
... instead, what I have currently is this:
using Terminator;
using Superman;
using Thor;
... which is what I do NOT want. I want to be able have a base namespace and sub-namespaces under that base. I don't know how to do this when creating an ATL service and what I need to modify to do this. Is it something I modify in the IDL file?
In case you are targeting managed clients it is possible to provide namespaces for them! However since COM is language independent you cannot provide namespaces using the interface description (type library). But whenever you are creating managed wrapper assemblies (that are actually referenced by the client), they can have namespaces to address the RCW objects. The keyword you are looking for is Primary Interop Assemblies. Those are assemblies that you as the vendor of the original library provide for clients to reference. To simplify this: You are doing the work, Visual Studio does for you when you are adding a reference to a COM library. You are creating the interop assembly and the customer does not reference the type library, but the assembly you generated. Using the tlbimp.exe tool it is possible to encapsulate the RCW types inside a namespace using the /namespace parameter.

moq internal interface in Silverlight 4. "Can not create proxy for types that are not accessible."

I try to mock an internal interface in Silverlight 4, using moq-silverlight 4.0.10827.0.
I get an error "Can not create proxy for types that are not accessible." in a Castle.DynamicProxy.Generators.GeneratorException.
I have [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] in the assemblyInfo of the tested assembly. I do not have any signed assemblies.
Try including the public key as well:
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
And make sure you also make the internals visible to the unit test assembly (assuming they're in a different assembly).
The InternalsVisibleTo switch works only in this scenario: You have to apply it in the assembly containing the internals you want to be visible to another assembly. If it would work when you specify it in an assembly who wants to consume those internals, then it would be a great security leak!