Before running this example, keep the following in mind:
You must be running at least Microsoft® .NET Framework 3.5 to use the WCF feature.
If you want to use WCF, the easiest way to do so is through the type-safe API.
WCF and .NET Remoting are not compatible in the same deployment project or component.
The example in this chapter requires both client and
server to use message sizes larger than the WCF defaults. For information
about changing the default message size, see the MSDN article regarding
setting of the maxreceivedmessagesize
property.
Deploying a WCF-based component requires the expertise of a .NET Developer because it requires performing a number of advanced programming tasks.
To deploy a WCF-based component, follow this general workflow:
Create your MATLAB® program and then test the code before implementing a type-safe interface. The functions in your MATLAB program must match the declarations in your native .NET interface.
In the following example, the deployable MATLAB code contains
one exported function, addOne
. The addOne
function
adds the value one (1) to the input received. The input must be numeric,
either a scalar or a matrix of single or multiple dimensions.
function y = addOne(x) % ADDONE Add one to numeric input. Input must be numeric. if ~isnumeric(x) error('Input must be numeric. Input was %s.', class(x)); end y = x + 1; end
addOne
must perform run-time type checking
to ensure valid input.
After you write and test your MATLAB code, develop an interface in either C# or Visual Basic that supports the native types through the API.
Define IAddOne Overloads. See Implement a Type-Safe Interface for complete rules on defining interface overloads.
In addition, when using WCF, your overloaded functions must have unique names.
Note that in the WCF implementation of addOne
,
you decorate the methods with the OperationContract
property.
You give each method a unique operation name, which you specify with
the Name
property of OperationContract
,
as in this example:
using System.ServiceModel; [ServiceContract] public interface IAddOne { [OperationContract(Name = "addOne_1")] int addOne(int x); [OperationContract(Name = "addOne_2")] void addOne(ref int y, int x); [OperationContract(Name = "addOne_3")] void addOne(int x, ref int y); [OperationContract(Name = "addOne_4")] System.Double addOne(System.Double x); [OperationContract(Name = "addOne_5")] System.Double[] addOne(System.Double[] x); [OperationContract(Name = "addOne_6")] System.Double[][] addOne(System.Double[][] x); }
As you can see, the IAddOne
interface specifies
six overloads of the addOne
function. Also, notice
that all have one input and one output (to match the MATLAB addOne
function),
though the type and position of these parameters varies.
For additional code snippets and data conversion rules regarding type-safe interfaces, see Implement a Type-Safe Interface.
For up-to-date information regarding WCF, see What Is Windows Communication Foundation on Microsoft webpage.
Compile IAddOne into an Assembly. Compile IAddOne.cs
into an assembly using Microsoft
Visual Studio®.
This example assumes your assembly contains only IAddOne
.
Realistically, it is more likely that IAddOne
will
already be part of a compiled assembly. The assembly may be complete
even before the MATLAB function is written.
Use either the Library Compiler app or the deployment command line tools to generate the type-safe API.
Using the Library Compiler. The Library Compiler app generates the type-safe API, when you build your component, if the correct options are selected.
Create your project.
When defining your project, use these values:
Project Name | AddOneComp |
Class Name | Mechanism |
File to compile | addOne |
Do not click the Package button at this time.
Expand the Additional Runtime Settings section.
On the Type-Safe API tab, do the following:
Select Enable Type-Safe API.
In the Interface assembly field, specify the location of the type-safe/WCF interface assembly that you built.
Select IAddOne
from the .NET
interface drop-down box. The interface name is usually
prefixed by an I
.
If the drop-down is blank, the Library Compiler app may have been unable to find any .NET interfaces in the assembly you selected. Select another assembly.
Specify Mechanism
, as the class
name you want the generated API to wrap, in the Wrapped
Class field.
Leave the Namespace field blank.
Build the project as usual by clicking the Package button.
Using the Deployment Command-Line Tools. To generate the type-safe API with your component build (compilation)
using mcc
, do the following:
Build the component by entering this command from MATLAB:
mcc -v -B 'dotnet:AddOneComp,Mechanism,3.5,private,local' addOne
See the mcc
reference
page in this for details on the options specified.
Generate the type-safe API by entering this command from MATLAB:
ntswrap -c AddOneComp.Mechanism -i IAddOne -a IAddOne.dll
where:
-c
specifies the namespace-qualified
name of the MATLAB
Compiler SDK™ assembly to wrap with a type-safe
API. If the component is scoped to a namespace, specify the full namespace-qualified
name (AddOneComp.Mechanism
in the example). Because
no namespace is specified by ntswrap
, the type-safe
interface class appears in the global namespace.
-i
specifies the name of the .NET
interface that defines the type-safe API. The interface name is usually
prefixed by an I
.
-a
specifies the absolute or relative
path to the assembly containing the .NET statically-typed interface,
referenced by the -i
switch.
If the assembly containing the .NET interface IAddOne
is
not in the current folder, specify the full path.
Not all arguments are compatible with each other. See the ntswrap
reference page for details
on all command options.
You have now built your component and generated a WCF-compliant type-safe API.
Next, develop a server program that provides access (via the WCFServiceContract
)
to the overloads of addOne
defined by the WCF IAddOne
interface.
The program references an App.config
XML configuration
file.
The WCF server program loads the WCF-based addOne.Mechanism
component
and makes it available to SOAP clients via the type-safe mechanismIAddOne
interface.
When writing your interface, you will be coding to handle jagged arrays, as opposed to rectangular arrays. For more information about jagged arrays, see Jagged Array Processing in this documentation.
Compile the server program using Microsoft Visual Studio by doing the following:
Create a Microsoft
Visual Studio project named AddMaster
.
Add AddMasterServer.cs
and App.config
(the
configuration file created in the previous step)
to your project.
Add references in the project to the following files.
This reference: | Defines: |
---|---|
IAddOne.dll | The .NET native type interface IAddOne |
MechanismIAddOne.dll | The generated type-safe API |
AddOneCompNative.dll | The generated assembly |
Unlike other .NET deployment scenarios, you do not need to reference MWArray.dll
in
the server program source code. The MWArray
data
types are hidden behind the type-safe API in MechanismIAddOne
.
If you are not already referencing System.ServiceModel
,
add it to your Visual Studio project.
Compile the program with Microsoft Visual Studio.
Run the server program from a command line.
The output should look similar to the following.
AddMaster Server is up running...... Press any key to close the service.
Pressing a key results in the following.
Closing service....
Configure your clients to communicate with the server by running
the automatic proxy generation tool, svcutil.exe
.
Most versions of Microsoft
Visual Studio can automatically generate
client proxy code from server metadata.
Before you generate your client proxy code using this step, the server must be available and running. Otherwise, the client will not find the server.
Create a client project in Microsoft Visual Studio.
Add references by using either of these two methods. See Port Reservations and Using localhost 8001 for information about modifying port configurations.
Method 1 | Method 2 |
---|---|
|
|
Port Reservations and Using localhost 8001. When running a self-hosted application, you may encounter issues with port reservations. Use one of the tools below to modify your port configurations, as necessary.
if You Run.... | Use This Tool to Modify Port Configurations.... |
---|---|
Windows® XP | httpcfg |
Windows Vista™ | netsh |
Windows 7 | netsh |
The client program differs from the AddMaster.cs
server
program as follows:
At start-up, this program connects to the AddMasterService
provided
by the AddMaster
WCF service.
Instead of directly invoking the methods of the type-safe
mechanism IAddOne
interface, the WCF client uses
the method names defined in the OperationContract
attributes
of IAddOne
.
Compile the client program by doing the following:
Add the client code (AddMasterClient.cs
)
to your Microsoft
Visual Studio project.
If you are not already referencing System.ServiceModel
,
add it to your Visual Studio project.
Compile the WCF client program in Visual Studio.
Run the client program from a command line.
The output should be similar to the following:
Conntecting to AddMaster Service through Http connection... Conntected to AddMaster Service... addOne(1) = 2 addOne(16) = 17 addOne(2) = 3 addOne(495) = 496 addOne([30 60 88]) = [31 61 89] addOne([0 2; 3 1]) = [1 3; 4 2] Press any key to close the client application.
Pressing a key results in the following.
Closing client....