I am working on an assignment and I am getting this error which I don't understand. I am writing a WCF client for a working service. Can anyone help me out here?
This line is throwing the error:
MyComplex sumcplx = proxy.complex_sum(one,two);
Error I get
Error: The best overloaded method match for 'NETProxyWCFClient.ProxyTypes.CalculatorClient.complex_sum(NETProxyWCFClient.ProxyTpes.MyComplex,NETProxyWCFClient.ProxyTpes.MyComplex)' has some invalid arguments
Code to my program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ServiceModel.Description;
namespace NETProxyWCFClient
{
[DataContract]
public class MyComplex
{
int real;
[DataMember]
public int Real
{
get { return real; }
set { real = value; }
}
[DataMember]
int im;
public int Im
{
get { return im; }
set { im = value; }
}
}
[ServiceContract]
interface ICalculator
{
[OperationContract]
int mult(int a, int b);
[OperationContract]
List<int> fib(int n);
[OperationContract]
MyComplex complex_sum(MyComplex a, MyComplex b);
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter in 2 numbers");
string read = Console.ReadLine();
string[] numbers = read.Split(' ');
int m = int.Parse(numbers[0]);
int n = int.Parse(numbers[1]);
ProxyTypes.CalculatorClient proxy = new ProxyTypes.CalculatorClient();
//Multiplcation
int sum = proxy.mult(m, n);
Console.WriteLine(sum.ToString());
//Mycomplex
MyComplex one = new MyComplex();
one.Im = m;
one.Real = n;
MyComplex two = new MyComplex();
two.Im = n;
two.Real = m;
MyComplex sumcplx = proxy.complex_sum(one,two);
Console.WriteLine(sumcplx.Im + " , " + sumcplx.Real);
//fib one
int[] listM = proxy.fib(m);
foreach (int listItem in listM)
{
Console.Write(listItem.ToString() + " ");
}
Console.WriteLine("");
//fib 2
int[] listN = proxy.fib(n);
foreach (int listItem in listN)
{
Console.Write(listItem.ToString() + " ");
}
Console.ReadLine();
}
}
}
If you have Added a service reference to your test program, there is a proxt class that is generated for you, so if you have ICalculator defined explicity (code you wrote) remove it. it is already in your project, under the NETProxyWCFClient.ProxyTpes.MyComplex,NETProxyWCFClient.ProxyTpes namespace.
Related
Ok, so I am trying to figure out why I am having a looping issue. The intention of the method GetNewDvdInfo() is to return a new dvd class with 5 properties and will be passed on to DvdController.cs in the CreateDvd() method and will then display all the dvds and the dvd the user added. The problem is that the GetNewDvdInfo() method is repeating itself, but when I was returning null instead, it was not looping.
DvdView.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DvdManager.Models;
/*
GetMenuChoice() : int
GetNewDvdInfo(): Dvd
DisplayDvd(Dvd dvd) : void
EditDvdInfo(Dvd dvd) : Dvd
SearchDvd() : int
ConfirmRemoveDvd(Dvd) : boolean
*/
namespace DvdManager.View
{
public class DvdView
{
public int GetMenuChoice()
{
string input;
int choice;
Console.Clear();
Console.WriteLine("Press 1 to display movies");
Console.WriteLine("Press 2 to add movie");
input = Console.ReadLine();
if (int.TryParse(input, out choice))
{
switch (choice)
{
case 1:
break;
case 2:
break;
default:
Console.WriteLine("Invalid input");
break;
}
}
return choice;
}
public Dvd GetNewDvdInfo() //looping here
{
string inputReleaseYear;
string inputRating;
int id = 4;
string readTitle;
int readReleaseYear;
string readDirector;
float readRating;
Console.WriteLine("What is the Title of the DVD?");
readTitle = Console.ReadLine();
Console.WriteLine("What is the Release Year of the DVD?");
inputReleaseYear = Console.ReadLine();
int.TryParse(inputReleaseYear, out readReleaseYear);
Console.WriteLine("Who is the Director of the DVD?");
readDirector = Console.ReadLine();
Console.WriteLine("What is the star rating of the DVD?");
inputRating = Console.ReadLine();
float.TryParse(inputRating, out readRating);
var dvd = new Dvd(id, readTitle, readReleaseYear, readDirector, readRating);
Dvd newDvd = GetNewDvdInfo();
return dvd;
}
public void DisplayDvd(Dvd dvd)
{
}
public Dvd EditDvdInfo(Dvd dvd)
{
return null;
}
public int SearchDvd()
{
return 0;
}
public Boolean ConfirmRemoveDvd(Dvd dvd)
{
return false;
}
}
}
DvdController.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DvdManager.Models;
using DvdManager.Data;
using DvdManager.View;
/*
Run() : void
Private CreateDvd(): void
Private DisplayDvds(): void
Private SearchDvds(): void
Private EditDvd() : void
Private RemoveDvd() : void
*/
namespace DvdManager.Controllers
{
public class DvdController
{
public DVDList _dvds = new DVDList();
public void Run()
{
Console.WriteLine("Welcome To Dvd Manager");
DvdView view = new DvdView();
var pass = view.GetMenuChoice();
if (pass == 1)
{
CreateDvd();
}
else if (pass == 2)
{
view.GetNewDvdInfo();
CreateDvd();
}
else
Console.WriteLine("Invalid.");
}
private void CreateDvd() //Create
{
var myView = new DvdView();
var dvdInfos = myView.GetNewDvdInfo();
List<Dvd> Dvds = _dvds.GetList();
Dvds.Add(new Dvd(0, "Batman", 2010, "Bruce", 4));
Dvds.Add(new Dvd(1, "Superman", 2009, "John", 4));
Dvds.Add(new Dvd(2, "Wonderwoman", 2012, "Omar", 4));
Dvds.Add(dvdInfos);
DisplayDvds();
}
private void DisplayDvds() //Read List<Dvd> dvds
{
List<Dvd> Dvds = _dvds.GetList();
for (int i = 0; i < Dvds.Count; i++)
{
Console.WriteLine(Dvds[i]);
}
RemoveDvd();
}
private void SearchDvds() //List
{
}
private void EditDvd(int id, Dvd dvd) //Update
{
}
private void RemoveDvd() //Delete int id
{
List<Dvd> Dvds = _dvds.GetList();
//Dvds.RemoveAt(Dvds[1]);
Dvds.Remove(Dvds.Single(x => x.Id == 1));
Console.WriteLine("Removed movie from list:");
for (int i = 0; i < Dvds.Count; i++)
{
Console.WriteLine(Dvds[i]);
}
}
}
}
You are making a recursive call at the end of GetNewDvdInfo(), just remove it.
You are calling this method recursively here:
var dvd = new Dvd(id, readTitle, readReleaseYear, readDirector, readRating);
Dvd newDvd = GetNewDvdInfo(); //!!!
return dvd;
Hence the looping. It seems like this line of code is not needed.
I am getting the "Fody/Alea.CUDA: clrobj(cGPU) does not have llvm" build error for a code in which I try to pass an array of struct to the NVIDIA Kernel using ALEA library. Here is a simplified version of my code. I removed the output gathering functionality in order to keep the code simple. I just need to be able to send the array of struct to the GPU for the moment.
using Alea.CUDA;
using Alea.CUDA.Utilities;
using Alea.CUDA.IL;
namespace GPUProgramming
{
public class cGPU
{
public int Slice;
public float FloatValue;
}
[AOTCompile(AOTOnly = true)]
public class TestModule : ILGPUModule
{
public TestModule(GPUModuleTarget target) : base(target)
{
}
const int blockSize = 64;
[Kernel]
public void Kernel2(deviceptr<cGPU> Data, int n)
{
var start = blockIdx.x * blockDim.x + threadIdx.x;
int ind = threadIdx.x;
var sharedSlice = __shared__.Array<int>(64);
var sharedFloatValue = __shared__.Array<float>(64);
if (ind < n && start < n)
{
sharedSlice[ind] = Data[start].Slice;
sharedFloatValue[ind] = Data[start].FloatValue;
Intrinsic.__syncthreads();
}
}
public void Test2(deviceptr<cGPU> Data, int n, int NumOfBlocks)
{
var GridDim = new dim3(NumOfBlocks, 1);
var BlockDim = new dim3(64, 1);
try
{
var lp = new LaunchParam(GridDim, BlockDim);
GPULaunch(Kernel2, lp, Data, n);
}
catch (CUDAInterop.CUDAException x)
{
var code = x.Data0;
Console.WriteLine("ErrorCode = {0}", code);
}
}
public void Test2(cGPU[] Data)
{
int NumOfBlocks = Common.divup(Data.Length, blockSize);
using (var d_Slice = GPUWorker.Malloc(Data))
{
try
{
Test_Kernel2(d_Slice.Ptr, Data.Length, NumOfBlocks);
}
catch (CUDAInterop.CUDAException x)
{
var code = x.Data0;
Console.WriteLine("ErrorCode = {0}", x.Data0);
}
}
}
}
}
Your data is class, which is reference type. Try use struct. Reference type doesn't fit Gpu well, since it require of allocating small memory on the heap.
I try to randomize the values if they are not changed, but it won't let me use the randomizer in the constructor and it gives an error when I use my other function.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Randomizer {
class Apartment {
public int height;
public int bas;
public int hasPent;
public Apartment(int b = 100, int h = 100, int p = 100) {
height = h;
bas = b;
hasPent = p;
public Room[,,] rooms = new Room[bas, bas, height];
finCon(bas, height, hasPent, rooms);
}
void finCon(int b, int h, int p, Room[,,] ro) {
Random r = new Random();
if (b==100) {
b = r.Next(2,4);
}
if (h==100) {
h = r.Next(4,15);
}
if (p==100) {
p = r.Next(0,20);
}
}
}
class Room {
int some = 37;
}
class Program {
static void Main(string[] args)
{
Apartment ap = new Apartment();
ap.finCon(ap.bas,ap.height,ap.hasPent,ap.rooms);
Console.WriteLine("{0}{1}",ap.bas,ap.height);
}
}
}
Errors:
(1:1) A namespace cannot directly contain members such as fields or methods
(16:25) } expected
(18:13) Method must have a return type
(18:23) Identifier expected
(18:31) Identifier expected
(18:40) Identifier expected
(18:47) Identifier expected
(21:9) A namespace cannot directly contain members such as fields or methods
(21:47) Identifier expected
(21:48) Identifier expected
(21:51) Expected class, delegate, enum, interface, or struct
(22:28) Expected class, delegate, enum, interface, or struct
(33:5) Type or namespace definition, or end-of-file expected
(46:1) Type or namespace definition, or end-of-file expected
I've made it compile:
namespace Randomizer
{
public class Apartment
{
public int height;
public int bas;
public int hasPent;
public Room[,,] rooms;
public Apartment(int b = 100, int h = 100, int p = 100)
{
height = h;
bas = b;
hasPent = p;
rooms = new Room[bas, bas, height];
finCon(bas, height, hasPent, rooms);
}
public void finCon(int b, int h, int p, Room[,,] ro)
{
Random r = new Random();
if (b == 100)
{
b = r.Next(2, 4);
}
if (h == 100)
{
h = r.Next(4, 15);
}
if (p == 100)
{
p = r.Next(0, 20);
}
}
}
public class Room
{
int some = 37;
}
class Program
{
static void Main(string[] args)
{
Apartment ap = new Apartment();
ap.finCon(ap.bas, ap.height, ap.hasPent, ap.rooms);
Console.WriteLine("{0}{1}", ap.bas, ap.height);
}
}
}
Your problem is trying to declare a property inside the constructor (you can't do it). I've made all classes public too.
Hope it will help you.
I am trying to learn web services. My problem is I have created a class "Item" as follows,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebServiceTaxCalc
{
public enum Type { BASIC, IMPORTED, EXEMPT}
public class Item
{
string name;
double basePrice;
int quantity;
Type TaxType;
public Item(string name, double price, int quantity, Type type)
{
this.basePrice = price;
this.name = name;
this.quantity = quantity;
this.TaxType = type;
}
public string getName()
{
return name;
}
public double getPrice()
{ return basePrice; }
public int getQuantity()
{ return quantity; }
public Type getType() { return TaxType; }
}
}
and another class for calculating the tax:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebServiceTaxCalc
{
public class ShoppingBasket
{
double itemsTotalPrice = 0.0;
double totalTax = 0.0;
public void addItem(List<Item> i)
{
foreach (Item a in i)
{
totalTax += taxPerItem(a);
double eachItemPrice = (a.getPrice() + taxPerItem(a));
itemsTotalPrice += eachItemPrice;
//Console.WriteLine(a.getQuantity() + " " + a.getName() + ": " + eachItemPrice);
}
//Console.WriteLine("sales tax: " + totalTax);
//Console.WriteLine("total cost: "+itemsTotalPrice);
}
public double taxPerItem(Item i)
{
double tax = 0;
if (i.getType() == Type.BASIC)
{
tax = i.getPrice() * 5 / 100;
return tax;
}
else if (i.getType() == Type.EXEMPT)
{
tax = 0;
return tax;
}
else
{
tax = i.getPrice() * 15 / 100;
return tax;
}
}
}
}
I am trying to pass the values to the web service and let the web service call the classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebServiceTaxCalc
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public ShoppingBasket Calc(string name, double price, int quantity, Type catg)
{
List<Item> l1 = new List<Item>();
Item i1 = new Item(name, price, quantity,catg);
l1.Add(i1);
ShoppingBasket sb = new ShoppingBasket();
sb.addItem(l1);
return sb;
}
}
}
I am giving the input like this,
input image:
and after invoking I am just getting this:
Output image
I am not getting the document tree of what I passed.
I have seen a useful post here https://stackoverflow.com/a/12039010/3768995
But I couldn't solve my problem.
Please guide me through this.
Add a [DataContract] attribute to ShoppingBasket and a [DataMember] attribute to the properties that need to be included when the response is sent. (And as was said, make them public properties.)
I'm a student trying to set up a WCF service. I got the host and client running with a servicelibrary, and it works fine until my client tries to call upon a service that accesses a library (Gamez) I've made. The method it calls upon works fine normally, it's just through the WCF that it suddenly crashes it.
So say it calls upon this:
public int AddValues(int a, int b)
{
int c = a + b;
return c;
}
It'll work fine, but this:
public Guid GetUserId(String userName)
{
Guid user = Gamez.Sql.GetIdFromUserName(userName);
return user;
}
=boom. Keep in mind that the GetIdFromUserName method works fine when called on from non-WCF-related code.
This is the client-side code:
namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
Service1Client client = new Service1Client();
client.Open();
String gameName = "Liksomspill";
String userName = "Grouse";
//int result = client.GetHighScore(userName, gameName);
//Console.WriteLine("highscoren er: {0}", result);
//Guid result = client.GetUserId(userName);
//Console.WriteLine("blablabla: {0}", result);
int a = 5;
int b = 2;
int result2 = client.AddValues(a, b);
Console.WriteLine("highscoren er {0}", result2);
Console.WriteLine();
Console.ReadLine();
client.Close();
}
}
}
This will work fine as long as it stays like this (so it'll return 7), but if I remove commenting from these lines:
Guid result = client.GetUserId(userName);
Console.WriteLine("blablabla: {0}", result);
the top line will throw an exception, but I have no idea what it is as it only says "FaultException`1 was unhandled" with subtext "The type initializer for 'Gamez.Sql' threw an exception.". I've tried looking this up, but I don't really understand how to fix this (I'm coding in Visual Studio 2012). Here are some of the links I found on the subject, but it's just massively confusing me:
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/1829b844-1237-4390-8d36-76a7e42a64d3/
http://sergecalderara.wordpress.com/2008/11/25/systemservicemodelfaultexception1-was-unhandled-by-user-code/
I'm sure there is just something basic which I am completely overseeing (...you are supposed to be able to call libraries with the wcf service, right? Or do I have to incorporate the parts of my library I want to use into the service library?). I've added references to the library so that shouldn't be a problem. I'm sorry if this question is kinda stupid, but I'm tearing myself apart in frustration after banging my head against the issue for six hours.
If you need some background information, I'm basically trying to set up an API for games uploaded to a website I'm making (so the games can extract/insert highscores, etc. from the database).
Thank you so much for your time.
If needed, here is the host code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using WcfServiceLibrary;
using System.ServiceModel.Description;
namespace WCFHost
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/WCF");
ServiceHost selfHost = new ServiceHost(typeof(Service1), baseAddress);
ServiceDebugBehavior debug = selfHost.Description.Behaviors.Find<ServiceDebugBehavior>();
if (debug == null)
{
selfHost.Description.Behaviors.Add(
new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{
if (!debug.IncludeExceptionDetailInFaults)
{
debug.IncludeExceptionDetailInFaults = true;
}
}
try
{
//selfHost.Open();
selfHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "Service1Service");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready");
Console.WriteLine("Press enter to terminate service");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occured: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
And here is the ServiceLibrary code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Gamez;
namespace WcfServiceLibrary
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public void SetHighScore(int score, String userName)
{
}
public int GetHighScore(String userName, String gameName)
{
int score = Gamez.Sql.getHighScore(userName, gameName);
return score;
}
public Guid GetUserId(String userName)
{
Guid user = Gamez.Sql.GetIdFromUserName(userName);
return user;
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public int AddValues(int a, int b)
{
int c = a + b;
return c;
}
}
}